Pusher.cfc - ColdFusion Component For Realtime Notification With HTML5 WebSockets

Posted July 21, 2010 at 10:55 AM by Ben Nadel

Tags: ColdFusion, Javascript / DHTML

Lately, I have been playing around a lot with a service called Pusher. Pusher provides realtime push notification services for your web applications using HTML5 WebSockets (or Flash for non-compatible browsers). While using the Pusher service is extremely simple, digitally signing your HTTP request is not; I've had a good deal of back and forth with the Pusher support team, trying to squash a few bugs in my code. Having finally figured out where my HEX-encoding was failing, I figured I would roll this up into a ColdFusion component and a tiny demo project that anyone could download.

 
 
 
 
 
 
 
 
 
 

The code for my Pusher.cfc ColdFusion component and my demo application is available on my Pusher.cfc project page. If you'd like to try the online demo, click here.




Reader Comments

Jul 21, 2010 at 11:51 AM // reply »
6 Comments

Hah... very timely. I've been digging through the docs on BlazeDS/LCDS for the last few days and really wishing Web Sockets were here today.


Jul 21, 2010 at 11:56 AM // reply »
10,638 Comments

@Brian,

People who have seen me play with the Pusher stuff have suggested that I look into BlazeDS - but I kind of love the idea of "as needed" communication rather than long polling.


Jul 21, 2010 at 12:57 PM // reply »
2 Comments

Ben, do you think could CF Gateways could perform a similar service as Pusher? Have you heard of anyone using CF Gateways with the HTML5 WebSockets?

We have a gateway set up to push IM messages to users and are intrigued at the possibility of doing the same thing with HTML5.


Jul 21, 2010 at 12:59 PM // reply »
10,638 Comments

@Paul,

I don't know how true this is anymore; but, I think at one time, pretty much all "Gateway" activity in ColdFusion was Enterprise edition only. As such, I never really played around with any of the gateway stuff. I think that may have been changed in recent editions of ColdFusion (I think Standard can do more gateway stuff).

I'll have to look into it one day.


Jul 21, 2010 at 1:11 PM // reply »
2 Comments

You're right and I always forget that. That probably explains why information for CF Gateways is so hard to come by.

Thanks!


Jul 21, 2010 at 2:37 PM // reply »
10,638 Comments

@Paul,

Yeah, but again, I think it has changed in recent releases. I just never really got into playing with it.


Jul 22, 2010 at 9:20 AM // reply »
15 Comments

Was the encoding issue the root cause of the 401 Unauthorized errors you were seeing? I have been busy with other projects lately and I haven't really had time to work on my pusher app. Looking forward to playing with it more.


Jul 22, 2010 at 9:58 AM // reply »
3 Comments

I've been looking for a similar solution online and this morning I found this http://www.ape-project.org I thought I could share with the CF community.

:)

Let me know your thoughts.


Jul 22, 2010 at 10:36 AM // reply »
10,638 Comments

@Robert,

Yeah it was. In 90% of the cases, the hex encoding resulted in the needed 64 characters. In 10% of the cases, it only resulted in 63 characters and I needed to pad with one zero... which I was foolishly padding on the *wrong* side of the encoded signature :(

@Elias,

Looks cool, but I can't use it - all of my servers are Windows; with all the little I know about VirtualBox stuff, it seems like too much overhead for me to play with at this time.


Jul 22, 2010 at 10:55 AM // reply »
3 Comments

@Ben Nadel,

Well in that case I think you are going to love this. This one looks very easy to implement.

Go to http://www.stream-hub.com/ and download the community edition

To Run the server
--------------------------
On Unix execute runExamples.sh,
on Windows runExamples.bat.

This will start StreamHub at http://localhost:7979/ -
open that page in your browser to view the examples.

Im on a Mac and I don't know how to execute the runExamples.sh script :(

Im going to try it on a windows box I will let you know the results.


Jul 22, 2010 at 12:34 PM // reply »
10,638 Comments

@Elias,

That also looks very cool. I'm really digging the realtime notification mindset. Now, I just gonna figure out what some of the better practices are.


Jul 22, 2010 at 3:04 PM // reply »
9 Comments

@Elias,

On a Mac, it should be:

> cd /wherever/you/stored/the/sh/file
> ./runExamples.sh

Or just:

> /wherever/you/stored/the/sh/file/runExamples.sh

-Joe


Jul 22, 2010 at 3:21 PM // reply »
3 Comments

@Joe Rinehart,

Thanks Joe. I actually got it to run.

I was looking into the JAVA samples they have. Im trying to send msgs to the StreamHub Server via CFC using the java API.

If anyone has any idea on how to implement this I'll truly appreciate it.


Jul 25, 2010 at 4:49 AM // reply »
1 Comments

Hey Ben - I was playing around with Pusher and was alerted to http://beaconpush.com/

You may want to check it out.

Way easier to use than Pusher (no need to complex key encoding/encryption) and works across all the major browser without needing Flash socket fallback.


Jul 25, 2010 at 5:08 PM // reply »
10,638 Comments

@Johans,

Looks cool. It appears there are quite a number of HTML5 Push services now. This stuff is really catching on (and getting easier and easier to implement).


Aug 18, 2010 at 3:40 PM // reply »
9 Comments

Ben,

I've tried to run the demo on your site and also download it and run from my system with my own API key, etc. and connecting to pusher just stays there. The connection_established event seems to never get fired. If I bypass the server connection test and post a message to the server, I see that the message is posted successfully, but none of the triggers are fired. I thought maybe it was my browser, so I updated to the latest version of Safari and still nothing, both on your demo and my locally installed demo. I'll keep trying to figure it out, but any help would be greatly appreciated. I know it's probably something really stupid and obvious that I'm missing.


Aug 18, 2010 at 4:05 PM // reply »
9 Comments

Well I just tried it from another PC and it worked! ... Not sure why this is happening?


Aug 18, 2010 at 5:26 PM // reply »
10,638 Comments

@Peter,

I've definitely seen the Pusher API just seem to give out from time to time. I just tried it on Safari on Mac (for the first time - was on a PC when I wrote this post) and it worked. Glad it finally got working for you.


Oct 29, 2010 at 11:28 AM // reply »
5 Comments

Have you tried using web sockets tied in with cfthread? In other words... Fire off 100 threads or so doing long requests, as each one finishes it pushes some data to the browser.

Would that work?


Nov 1, 2010 at 9:11 PM // reply »
10,638 Comments

@Steve,

I've played around with integrating Pusher into jQuery as part of a long-running AJAX request:

http://www.bennadel.com/blog/2017-Proof-Of-Concept-Adding-Pusher-Powered-Update-Support-To-jQuery-AJAX.htm

Basically, I added an "update" callback for the $.ajax() method which allows communication to take place after the AJAX request has completed.

Now, granted, in that example, I am not making any communication in an actual CFThread - but there's not that much difference to do something from within a CFThread. At the end of the day, you'll just be posting to the PusherApp API using CFHTTP.


Jul 25, 2011 at 5:52 AM // reply »
1 Comments

Hi, the demo online doesnt work...


Sep 7, 2011 at 7:15 PM // reply »
3 Comments

Ben,

Have you worked with the presence and private channel auth at all with pusher? I exchanged an email with Bradley Lambert who said he's not really worked with it since he posted the cfc so it's probably his pusher-cfc will probably not get developed further. Wonder if perhaps we could expand on it to provide the presenceAuth and and socketAuth capabilities like the php library has in your cfc.

I've personally been struggling with the SHA-HMAC256 signing of the JSON message, or I'd add myself. (CF9 Standard).

I'm seriously about to do a cfhttp call to the dang PHP component to work around for now&(ugh) but at least it's working.

It's simply a matter for presence to sign

  • <socket_id>:<channel_name>:<JSON encoded user data>

and return Like

  • {"auth":"278d425bdf160c739803:afaed3695da2ffd16931f457e338e6c9f2921fa133ce7dac49f529792be6304c","channel_data":"{\"user_id\":10,\"user_info\":{\"name\":\"Mr. Pusher\"}}"}

I do see the merit behind this process and notice the authenticated private channels are absent in other push "services" out there hence my continued interest with the Pusher service as an option for a project I'm working on.

The details for pusher's auth signature is here:
http://pusher.com/docs/auth_signatures

Regards,

Jim


Nov 17, 2011 at 11:55 AM // reply »
1 Comments

Hi Ben,

When I change the id, key, and secret it doesn't work anymore. Any idea?

Great code BTW.

Yacine


Dec 7, 2011 at 6:00 PM // reply »
5 Comments

Jim did you ever get that to work? I'm running into the exact same problem.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 3, 2012 at 10:49 PM
How I Got Node.js Running On A Linux Micro Instance Using Amazon EC2
Wow this was really helpful! Only thing I would add is you need to update your .bash_profile after you edit the secure_path. This is what I did: $ . ~/.bash_profile Otherwise, NPM won't be found. ... read »
Feb 3, 2012 at 10:14 PM
Pushing Base64-Encoded Images Over HTML5 WebSockets With Pusher And ColdFusion
@Ben, Just wanted to let you know that pusher are soon to start limiting sizes on messages. This was the detail that came through in the Feb dispatch: "However, we will soon be limiting the s ... read »
Feb 3, 2012 at 5:05 PM
Regular Expressions Make CSV Parsing In ColdFusion So Much Easier (And Faster)
I tried using your RegEx in my C# program, but it was matching an extra empty-string at the end and so I would end up with an extra field that doesn't exist, so I changed it to this: (^|,)("(?: ... read »
Feb 3, 2012 at 3:47 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
Josh Cyr posted this on Twitter just a little bit ago. Thought it was appropriate. http://stackoverflow.com/questions/1619152/how-to-create-rest-urls-without-verbs/1619677#1619677 ... read »
Feb 3, 2012 at 2:28 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
@Michael, You definitely make a good point (and extra points for quoting movies - I love movies). When you use a return() statement to define the object's public API, it does provide a consistent a ... read »
Feb 3, 2012 at 2:04 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
To quote Jurassic Park: "Just because you can doesn't mean you should". I completely, utterly disagree with the thought that this is more readable. Consider the current module pattern: if ... read »
Feb 3, 2012 at 1:10 PM
REST API Design Rulebook By Mark Masse
@Jordan, Yeah, WRML was created by Mark Masse (author of the book). I also found it to be a bit convoluted. I suppose it is intended to allow the Client to be able to programmaticaly respond to cha ... read »
Feb 3, 2012 at 1:08 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
@Jason, To be honest, I don't have good answers for that kinds of stuff. And, to the point, that is specifically why I *really* liked the REST API Design Rulebook by Mark Masse - he just cuts throu ... read »