Using Faye to implement a publish-subscribe design pattern

Screen shot 2013-02-20 at 11.09.52 AM

My last blog post, Example of a Real-Time Web App, was an overview of the architecture of WhitePages’s Mailer.  We used Ruby on Rails, Spine.js, Faye, and Sidekiq to build an asynchronous web app.  Rails is a server-side MVC framework, Spine.js is a client-side MVC framework, Sidekiq processes jobs, and Faye ties those three pieces together with HTTP server push.  This blog dives deeper into Faye, which can be the basis for implementing a publish-subscribe design pattern.

What is Faye?

In their own words:

“Faye is a publish-subscribe messaging system based on the Bayeux protocol. It provides message servers for Node.js and Ruby, and clients for use on the server and in all major web browsers.”

In my words:

“Faye is an abstraction layer that simplifies HTTP server push.”

Faye is one of many options for abstracting the details of server push.  Popular alternatives include hosted solutions like Pusher.com and PubNub, and self-hosted solutions like Socket.io and Firehose.  Choosing the best fit for your project is an exercise left to the reader.

How does it work?

Faye consists of two pieces: a subscriber and a publisher.  Both the subscribing and publishing components are Faye clients.  For basic server-to-browser pushing, the browser client (in JavaScript) subscribes to the server client (in Ruby) – the server then broadcasts to a channel that the client is listening to.

Get Started with Faye on Rails

To get started, run a Faye server mounted at ‘/faye’.  Getting your server up is well-documented on Faye’s site.  With a Faye server up and running, include the Faye javascript code in your project and create a client listening to the default port (9292) with the following code:

$(function() {
var faye_subscriber = new Faye.Client(‘http://localhost:9292/faye’);
});

To create a Faye client on the Ruby server, the syntax is similar:

faye_publisher = Faye::Client.new(‘http://localhost:9292/faye’)

Now the client can subscribe, here we’ll define a channel called “updates” and alert whatever data is broadcast to the channel:

faye_subscriber.subscribe(‘/updates’, function (data) {
alert(data);
});

And the server can publish data – we’ll publish the message “hello push!”.

faye_publisher.publish(‘/updates’, “hello push!”)

That’s all there is to it.  After publishing “hello push!”, the client-side subscriber hears it and alerts the data.

How’d we use it?

When a user imports their friends from Facebook, we process each friend in a background job and publish the results as friends are processed.  The published message includes everything the UI needs to display it, so we can update users’ lists without waiting for the whole job to finish.  As soon as a friend is processed, the user gets it.

Conclusion

Pub/sub, in symphony with other real-time web technologies, allows you to close the performance gap between native and web apps, and Faye makes it dead simple to get started.

jclement@whitepages.com'

by Jared Clement

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>