Faye is an implementation of the Bayeux protocol in Ruby. It includes a server backend and an adapter for Rack, and a self-contained JavaScript client. Bayeux is a protocol designed to facilitate Comet, also known as ‘server push’ or ‘Ajax push’, a technique for pushing messages from the server to the client to minimize latency.
NOTE this is not currently intended for large-scale use. It is being developed mostly as a tool for scripting multiple browsers and automating JavaScript testing. However, use of a well-known protocol should allow it to be repurposed fairly easily.
Faye can be installed as middleware in front of any Rack application. For example, here’s a config.ru
for running it with Sinatra:
require 'rubygems' require 'faye' require 'sinatra' require 'path/to/sinatra/app' use Faye::RackAdapter, :mount => '/comet', :timeout => 30 run Sinatra::Application
This starts a Comet endpoint at http://localhost:9292/comet
with the client script at http://localhost:9292/comet.js
. The timeout
option sets how long (in seconds) a long-polling request will wait before timing out; this must be less than the timeout set on your frontend web server so that the Comet server can send a response before Apache (for example) closes the connection.
In your front-end code, set up the client as follows:
<script type="text/javascript" src="/comet.js"></script> <script type="text/javascript"> CometClient = new Faye.Client('/comet'); CometClient.connect(); </script>
This client object can then be used to publish and subscribe to named channels as follows:
CometClient.subscribe('/path/to/channel', function(message) { // process received message }); CometClient.publish('/some/other/channel', {foo: 'bar'});
You can publish arbitrary JavaScript objects to a channel, and the object will be transmitted as the message
parameter to any subscribers to that channel. Channel names must be formatted as absolute path names as shown. Channels beginning with /meta/
are reserved for use by the messaging protocol and may not be subscribed to.
See demo apps in examples
. If you’ve checked this out from the repo rather than installing the gem you will need to build the JavaScript client from source, this is done as follows:
sudo gem install jake jake -f
-
Provide support for user-defined
/service/*
channels -
Support Thin’s async response
-
Allow server to scale to multiple nodes
-
Provide a server-side client
-
Console for scripting browsers from the command line
sudo gem install hoe faye
(The MIT License)
Copyright © 2009 James Coglan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.