Playing with Pusher


A couple of weeks ago Ben Nadel did a few excellent blog posts about Pusher, a service in beta stages that offers “real time client push to HTML5 browsers” that support web sockets and falls back to a flash plug-in on browsers that do not support web sockets.

This evening I finally had a few minutes to play around with it between working on other projects.  I thought it would be a bit of an ordeal getting things going, but it was surprisingly fast.  Pusher is in open beta so getting an account setup took all of a minute (literally).

Since that was far too easy, the next hurdle would be communicating with the service.  If you check Ben’s demo code quite a bit of it was more or less preparing the data to be sent to Pusher so it could be pushed to the clients.  Ben had a link to pusher.cfc by Bradley Lambert which actually alleviated the hassle.

That is the heavy lifting of setting up your pusher app.  I did actually write a few lines of code to setup the remote facade for interacting with pusher.cfc, aside from that everything just worked out of the gate.  For those interested, the remote facade for pusher.cfc.

<cfcomponent output="false">

 <cffunction name="push" access="remote" returntype="any" returnformat="json">
  <cfargument name="channel" type="string" required="true">
  <cfargument name="event" type="string" require="true">
  <cfargument name="data" type="any" required="true">
  <cfargument name="socketID" type="string" required="false" default="">
  <cfscript>
   var push = getPusher().triggerPush(arguments.channel, arguments.event, serializeJSON(arguments.data), arguments.socketID);
   return push;
  </cfscript>
 </cffunction>

 <cffunction name="getPusher" access="private" returntype="any">
  <cfscript>
   var pusher = createObject("component", "com.pusher");
   return pusher;
  </cfscript>
 </cffunction>

</cfcomponent>

I bound to a few simple events on a test page so I could make sure everything was working, but I did not get much further this evening.  Before I started going wild binding to events, I wanted to take a little time and think things out a little bit.  What I will say is, if you are looking for real time client push and you don’t feel like dealing the the added complexity of running Blaze/LCDS or the other alternatives, I cannot think of a better way of spending 30 minutes.

Advertisements

About Robert Zehnder

Web application developer specializing in ColdFusion/Railo and Open Source development.

Posted on July 15, 2010, in ajax, ColdFusion, Pusher. Bookmark the permalink. 4 Comments.

  1. Awesome stuff. Agreed that wrapping the Pusher code into a CFC makes life so much simpler. In my most recent playings, I have taken my version of the code and just put into a CFC that has two methods – init() and pushMessage(). Glad you’re liking this stuff – it’s very exciting technology.

    • It is definitely some good stuff. Your chat application demo did a good job covering event binding and the basic concepts so it made things pretty easy. What I am trying to emulate a client/server environment although I do not know how well it will work. The response time leaves me very optimistic. Very exciting indeed.

  2. Yeah, the response times are sweet! Although, right now, I am having weird, occassional 401 Unauthorized errors that I cannot figure out. I have posted in their support forum, so hopefully they can help me debug it.

    • I have seen several instances when a pushed message has not shown up on the other clients and just kinda shrugged it off. I should throw some error checking in my ajax call to see if this is perhaps the same error.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: