Private Pub is a Ruby gem for use with Rails to publish and subscribe to messages through Faye. It allows you to easily provide real-time updates through an open socket without tying up a Rails process. All channels are private so users can only listen to events you subscribe them to.
Watch RailsCasts Episode 316 for a demonstration of Private Pub.
Add the gem to your Gemfile and run the
bundle command to install it. You'll probably want to add "thin" to your Gemfile as well to serve Faye.
Run the generator to create the initial files.
rails g private_pub:install
Next, start up Faye using the rackup file that was generated.
rackup private_pub.ru -s thin -E production
//= require private_pub
In Rails 3.0 add the generated private_pub.js file to your layout.
It's not necessary to include faye.js since that will be handled automatically for you.
subscribe_to helper method on any page to subscribe to a channel.
<%= subscribe_to "/messages/new" %>
<% publish_to "/messages/new" do %>$("#chat").append("<%= j render(@messages) %>");<% end %>
If you prefer to work through JSON instead of
.js.erb templates, you can pass a hash to
publish_to instead of a block and it will be converted
to_json behind the scenes. This can be done anywhere (such as the controller).
PrivatePub.publish_to "/messages/new", :chat_message => "Hello, world!"
The configuration is set separately for each environment in the generated
config/private_pub.yml file. Here are the options.
server: The URL to use for the Faye server such as
secret_token: A secret hash to secure the server. Can be any string.
signature_expiration: The length of time in seconds before a subscription signature expires. If this is not set there is no expiration. Note: if Faye is on a separate server from the Rails app, the system clocks must be in sync for the expiration to work properly.
How It Works
subscribe_to helper will output the following script which subscribes the user to a specific channel and server.
The signature and timestamp checked on the Faye server to ensure users are only able to access channels you subscribe them to. The signature will automatically expire after the time specified in the configuration.
publish_to method will send a post request to the Faye server (using
Net::HTTP) instructing it to send the given data back to the browser.
Serving Faye over HTTPS (with Thin)
To server Faye over HTTPS you could create a thin configuration file
config/private_pub_thin.yml similar to the following:
---port: 4443ssl: truessl_key_file: /path/to/server.pemssl_cert_file: /path/to/certificate_chain.pemenvironment: productionrackup: private_pub.ru
certificate_chain.pem file should contain your signed certificate, followed by intermediate certificates (if any) and the root certificate of the CA that signed the key.
Next reconfigure the URL in
config/private_pub.yml to look like
Finally start up Thin from the project root.
thin -C config/private_pub_thin.yml start
Unfortunately I have not had time to actively work on this project recently. If you find a critical issue where it does not work as documented please ping me on Twitter and I'll take a look.
Development & Feedback
Questions or comments? Please use the issue tracker. Tests can be run with