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 »
11,307 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 »
11,307 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 »
11,307 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 »
11,307 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 »
11,307 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 »
11,307 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 »
11,307 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 »
11,307 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
Jun 17, 2013 at 9:36 PM
Object Thinking By David West
@Jonah, Please, don't feel bad at all. I appreciate all that you have contributed to the conversation. And, the more points of view I get, the more confident I am that I will some day, some how und ... read »
Jun 17, 2013 at 9:32 PM
Object Thinking By David West
@Paul, I definitely have a mental hurdle when it comes to discovering better design over time. My brain has this insane urge to just understand how you do something right the first time :) But, eve ... read »
Jun 17, 2013 at 9:29 PM
SOTR 2013 - The Best Conference I Never Went To
I just had to watch this again - amazing :) ... read »
Jun 17, 2013 at 9:28 PM
Working With Inherited Collections In AngularJS
@Ali, You are right - it is confusing. I should have just named it "saveForm()" or "submitForm()" or something to that effect. Then, the saveForm() method could have simply vali ... read »
Jun 17, 2013 at 9:27 PM
Working With Inherited Collections In AngularJS
@Samuel, Good question - that was also bothering me when I wrote the code. Yes, I could have moved it up into AppController. The reason that I didn't for this demo was that I didn't want the AppCon ... read »
Jun 17, 2013 at 9:23 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Josh, My pleasure. It was fun to learn more about this stuff. @Chebby, Will do - we're gonna be moving some stuff over to S3, so I am sure I'll be learning all sorts of interesting things / use ... read »
Jun 17, 2013 at 4:21 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Thanks Ben. Would love to see more CF/S3 examples! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools