Faye

Simple pub/sub messaging for the web

Ruby server

Setting up

All Faye clients need a central messaging server to communicate with; the server records which clients are subscribed to which channels and handles routing of messages between clients. Faye is just like any other Rack app, and you start it by making a rackup file:

# config.ru

require 'faye'
bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
run bayeux

Faye runs on MRI and JRuby, and requires at least Ruby 1.9.

The RackAdapter class supports these options during setup:

  • :mount – the path on the host at which the Faye service is available. In this example, clients would connect to http://localhost:9292/faye to talk to the server. The server will handle any request whose path begins with the :mount path; this is so that it can interoperate with clients that use different request paths for different channels.
  • :timeout – the maximum time to hold a connection open before returning the response. This is given in seconds and must be smaller than the timeout on your frontend webserver. Faye uses Thin as its webserver, whose default timeout is 30 seconds.
  • :engine – (optional) the type and parameters for the engine you want to use – see the engines documentation
  • :ping – (optional) how often, in seconds, to send keep-alive ping messages over WebSocket and EventSource connections. Use this if your Faye server will be accessed through a proxy that kills idle connections.

It also allows WebSocket extensions to be plugged in. For example, to enable permessage-deflate for supporting clients:

require 'faye'
require 'permessage_deflate'

bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
bayeux.add_websocket_extension(PermessageDeflate)

You can use any extension that’s compatible with the websocket-extensions framework.

Faye can also be setup as Rack middleware, for example in front of a Sinatra application:

# config.ru

require 'faye'
require File.expand_path('../app', __FILE__)

use Faye::RackAdapter, :mount => '/faye', :timeout => 25

run Sinatra::Application

When set up as middleware, extensions can be added using a block that is called with the middleware instance once it is created:

use Faye::RackAdapter, :mount => '/faye', :timeout => 25 do |bayeux|
  bayeux.add_extension(MyExtension.new)
  bayeux.add_websocket_extension(PermessageDeflate)
end

Running your Faye application

Faye supports the same set of servers as faye-websocket. The faye-websocket documentation has plenty of information on running Rack applications with these servers (see Running your socket application), but it’s worth restating here that for some servers you must tell Faye::WebSocket which web server you’re using before booting the application, so it can load adapters for it. For example to load the Thin adapter:

# config.ru

require 'faye'
Faye::WebSocket.load_adapter('thin')

app = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)

run app

Some servers supported by Faye, such as Passenger, use a multi-process model rather than using threads or events in the same process. For those servers, the default in-memory engine will not work; you should use a multi-process engine such as the Redis backend.

Faye uses MultiJson to allow fast JSON processing on both MRI and JRuby. For speed, we recommand using the :oj engine on MRI and the :gson engine on JRuby.