Pusher.cfc - ColdFusion Component For Realtime Notification With HTML5 WebSockets
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
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.
@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.
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.
@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.
You're right and I always forget that. That probably explains why information for CF Gateways is so hard to come by.
Thanks!
@Paul,
Yeah, but again, I think it has changed in recent releases. I just never really got into playing with it.
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.
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.
@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.
@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.
@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.
@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
@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.
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.
@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).
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.
Well I just tried it from another PC and it worked! ... Not sure why this is happening?
@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.
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?
@Steve,
I've played around with integrating Pusher into jQuery as part of a long-running AJAX request:
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.
Hi, the demo online doesnt work...
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
and return Like
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
Hi Ben,
When I change the id, key, and secret it doesn't work anymore. Any idea?
Great code BTW.
Yacine
Jim did you ever get that to work? I'm running into the exact same problem.