Pushing events to your iPhone using WebSockets and Pusher
Earlier today, New Bamboo announced their new Pusher service. Pusher is a centralized service that allows you to distribute real-time events from your web apps to the browser using HTML5 WebSockets.
It immediately struck me: why limit yourself to the browser? What if your iPhone (or iPad) app could receive those events too? Sure, you could use the Apple Push Notification Service, but why go through all the hassle that entails just to send events from your server?
If you could use the same event-distribution mechanism for your real-time HTML5 browser interface as your iPhone/iPad (or any other) interface, that's an instant win.
Getting started with the Pusher Objective-C client
So, with just the existing Javascript reference client to go on, I set about trying to create an Objective-C client. I've tried to mirror the Javascript API where it makes sense, but using a more Cocoa-centric approach (e.g. target/selector binding and notifications).
Using the library is fairly straightforward and there are principally two ways of using it, which I'll explain here. If you haven't already, you might want to read up on how Pusher works.
First of all, you need to create an instance of PTPusher
for the channel you want to monitor.
Once you have a PTPusher
instance, you can start registering for events. Each event listener requires a target
and a selector
.
The event callback method will receive a single argument, the PTPusherEvent
object representing the event. Here's what handleEvent:
might look like:
As you can see, PTPusherEvent
has two properties: name
and data
. The data
property will return the deserialized JSON data as a native Objective-C object, typically an NSDictionary.
The second approach is to use notifications. Whenever PTPusher receives an event, it will post a PTPusherEventReceivedNotification
. This will allow you to respond to events across your application without necessarily knowing about a specific PTPusher
instance:
In your notification handler, you will be able to get the PTPusherEvent
from the NSNotification's object
property.
Because your notification handler might not know anything about the PTPusher
that sent it, you might want to check the channel from which the event arrived before deciding how to handle it. You can do this by using the channel
property of PTPusherEvent
.
Obviously, both Pusher and the Objective-C client are still in their early stages. There is still some work to be done with the client, including proper error handling, but there's a lot of potential to create some interesting, cool applications using this. I already have some plans to integrate it with the work I've been doing on synching web services with Core Data.
I've recorded a small screencast that shows the library in action. Grab yourself a Pusher account and API key and have a play. As always, the code is available on GitHub.