Get Real with Pusher! Getting Those Alert Birds Squawking...
Pusher rocks! It gets real with the web in a major way. Websockets! I'm the new kid on the Loggly block and I've been working on Loggly's first alerting app, Alert Birds. Alert Birds uses Pusher because it's fast (real time is kinda hard to beat) and it's easy to use.
I'm giving you guys some code to play around with. Using Pusher and Sound Manager, we're going to set up a site that makes Loggly's Alert Birds hop and sing on a page whenever anyone clicks on them. That means you see when your buddies click birds, and vise verse. Just think of it as a bird chatroom. Trust me, it's awesome.
If you want to follow along with the code, clone the github repo I created here:
All you need to do in order to get pusher working is sign up for an account, grab some credentials and get coding.
Pusher has two main components:
- Listening for events.
- Triggering events.
Let's start with listening:
To listen for a pusher event you only need the app key, channel name and event name. With these compoenets listening to a Pusher channel is pretty straight forward and looks something like this:
Triggering is just as straight forward and requires the same elements as listening, plus the app secret and a backend to handle and send data to Pusher.
First you'll need some data to send, let's go nuts with ajax!
This brings us to the backend component of triggering events, so let's look at the trigger.php file.
The first thing you need to do is head over to pusher's Publisher Libraries and choose the backend language you heart the most.
You can find Pusher's list of Publisher Libraries here: http://pusher.com/docs/rest_libraries
Once you do that simply follow the Library authors instructions and go nuts! In my case I chose the generic php library to demonstrate this simple example. It's all fairly straight forward but I did run into a namespace issue with my host. To fix it I simply commented out the namespace in Pusher.php.
In trigger.php I add Pusher.php using php's require function, put my keys, secrets etc into variables and use Pusher.php's Pusher() class to validate. The last line triggers the event and grabs data from the button variable I sent to the URL using my ajax call.
I should point out that Pusher is somewhat flash dependent as well. For browsers that don't support websockets, they have a solution. Flash. Which means it's probably a good idea to handle flashblockers for any pusher app you create.
Here's Pusher's list of supported browsers: http://pusher.com/docs/browser_compatibility
At this point you should have everything you need to communicate with our Alert Birds. Go nuts!