Faye gets server-side clients

After its initial release a few weeks ago, the number one feature requested for Faye has been server-side clients. The first version let JavaScript running in the browser send messages, but now you can create a client on the server side to let your backend applications subscribe and publish messages. We’ve got clients for Node.js and Ruby, the latter requiring EventMachine.

Both environments can use ‘local’ clients, that is clients in the same process as the server, and remote clients that connect via HTTP. For example, under Node.js:

// Remote client
client = new Faye.Client('http://example.com/faye');

// Local client
server = new Faye.NodeAdapter(options);
client = server.getClient();

The client works exactly the same as the one you’ve been using in the browser; indeed it’s the exact same class with some different adapters to handle Node’s HTTP APIs. Under Ruby:

# Remote client
client = Faye::Client.new('http://example.com/faye')

# Local client
server = Faye::RackAdapter.new(options)
client = server.get_client

EM.run {
  client.subscribe('/from/*') do |message|
    # do something with message hash

  client.publish('/from/jcoglan', 'hello' => 'world')

There’s also been a bunch of bugfixes, and the Node bindings have been updated to run on the latest node release (it should run under 0.1.29 and 0.1.30 as of this writing). Grab it on RubyGems.org or from the GitHubs.

If you’ve enjoyed this article, you might enjoy my recently published book JavaScript Testing Recipes. It’s full of simple techniques for writing modular, maintainable JavaScript apps in the browser and on the server.