Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Handle pub/sub messaging through private channels in Rails using Faye.
Ruby JavaScript
tag: 0.1.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Private Pub

This is a Ruby gem for use with Rails to publish and subscribe to messages through a separate server such as Faye. All channels are automatically made private.


Add the gem to your Gemfile and run bundle.

gem "private_pub"

Run the generator to create the initial files.

rails g private_pub:install

Add the generated JavaScript to your layout file. Currently this uses jQuery, so you will need to customize it if you aren't using jQuery.

<%= javascript_include_tag "private_pub" %>

Next, install and start up Faye using the rackup file that was generated.

gem install faye
rackup -s thin -E production

It's not necessary to add the faye.js since that will be handled automatically.


Use the subscribe_to helper method on any page to subscribe to a channel.

<%= subscribe_to "/messages/new" %>

Use the publish_to helper method to publish messages to that channel. If a block of JavaScript is passed it will be evaluated automatically. This is usually done through a JavaScript AJAX response.

<% publish_to "/messages/new" do %>
  $("#chat").append("<%= escape_javascript render(@messages) %>");
<% end %>

There will be alternative ways to publish/subscribe to messages in the future.


Security is handled automatically for you. Only the Rails app is able to publish. Users are only able to receive messages on the channels you subscribe them to. This means every channel is private.

Here's how it works. The subscribe_to helper will output an element containing data information about the channel.

<span class="private_pub_subscription" data-channel="/messages/new" data-signature="2aae6c35c94fcfb415dbe95f408b9ce91ee846ed" data-timestamp="13019431281234"></span>

The data-signature is a combination of the channel, timestamp, and secret token set in the Rails app. This is checked by the Faye extension when subscribing to a channel to ensure the signature is correct. The signature is automatically expired after 1 hour but this can be configured.

PrivatePub.signature_expiration = 10.minutes

Or nil for no expiration. Note: if Faye is on a separate server from the Rails app it's important that the time is in sync.

Something went wrong with that request. Please try again.