• This repository has been archived on 12/Dec/2021
  • Stars
    star
    864
  • Rank 52,774 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Handle pub/sub messaging through private channels in Rails using Faye.

Unmaintained

The Private Pub gem is no longer maintained. Feel free to fork this project.

Private Pub

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.

Setup

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.

gem "private_pub"
gem "thin"

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

In Rails 3.1 add the JavaScript file to your application.js file manifest.

//= require private_pub

In Rails 3.0 add the generated private_pub.js file to your layout.

<%= javascript_include_tag "private_pub" %>

It's not necessary to include faye.js since that will be handled automatically for you.

Usage

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 send JavaScript to that channel. This is usually done in a JavaScript AJAX template (such as a create.js.erb file).

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

This JavaScript will be immediately evaluated on all clients who have subscribed to that channel. In this example they will see the new chat message appear in real-time without reloading the browser.

Alternative Usage

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!"

And then handle this through JavaScript on the client side.

PrivatePub.subscribe("/messages/new", function(data, channel) {
  $("#chat").append(data.chat_message);
});

The Ruby subscribe_to helper call is still necessary with this approach to grant the user access to the channel. The JavaScript is just a callback for any custom behavior.

Configuration

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 http://localhost:9292/faye.
  • 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

The subscribe_to helper will output the following script which subscribes the user to a specific channel and server.

<script type="text/javascript">
  PrivatePub.sign({
    channel: "/messages/new",
    timestamp: 1302306682972,
    signature: "dc1c71d3e959ebb6f49aa6af0c86304a0740088d",
    server: "http://localhost:9292/faye"
  });
</script>

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.

The 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: 4443
ssl: true
ssl_key_file: /path/to/server.pem
ssl_cert_file: /path/to/certificate_chain.pem
environment: production
rackup: private_pub.ru

The 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 https://your.hostname.com:4443/faye

Finally start up Thin from the project root.

thin -C config/private_pub_thin.yml start

Project Status

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 bundle and rake commands.

More Repositories

1

cancan

Authorization Gem for Ruby on Rails.
Ruby
6,265
star
2

ruby-warrior

Game written in Ruby for learning Ruby.
Ruby
3,830
star
3

letter_opener

Preview mail in the browser instead of sending.
Ruby
3,686
star
4

dotfiles

config files for zsh, bash, completions, gem, git, irb, rails
Shell
2,312
star
5

nifty-generators

A collection of useful Rails generator scripts.
Ruby
1,984
star
6

nested_form

Rails plugin to conveniently handle multiple models in a single form.
Ruby
1,792
star
7

railscasts-episodes

NOT MAINTAINED. See README.
Ruby
846
star
8

railscasts

railscasts.com in open source (outdated).
Ruby
760
star
9

populator

Mass populate an Active Record database.
Ruby
393
star
10

complex-form-examples

Various ways to handle multi-model forms in Rails.
Ruby
304
star
11

trusted-params

Rails plugin for overriding attr_accessible protection.
Ruby
149
star
12

mustard

Simple "must" expectations for tests and specs in Ruby.
Ruby
144
star
13

govsgo

Rails 3 app for playing the board game Go online.
Ruby
140
star
14

xapit

High level Ruby library for interacting with Xapian, a full text search engine.
Ruby
139
star
15

rails-templates

Template scripts for creating new rails applications.
Ruby
134
star
16

cocoa-web-app-example

A Cocoa application to demonstrate the interaction between Objective-C and JavaScript in a WebView.
Objective-C
96
star
17

importex

Import an Excel file using Ruby.
Ruby
90
star
18

uniquify

Generate a unique, random token for Active Record.
Ruby
87
star
19

textmate-themes

My TextMate themes (includes Railscasts theme)
69
star
20

acts-as-list

NOT MAINTAINED. Gem version of acts_as_list Rails plugin.
Ruby
65
star
21

abingo

Fork of A/Bingo plugin for Rails.
Ruby
55
star
22

railscasts-scripts

Scripts used internally when producing RailsCasts
Ruby
52
star
23

scope-builder

Build up named scopes conditionally.
Ruby
51
star
24

rmov

Ruby wrapper for the QuickTime C API.
C
48
star
25

render-caching

Cache render calls in Rails controllers.
Ruby
45
star
26

enlighten

Interactive ruby debugger in the browser.
Ruby
41
star
27

static_actions

Rails plugin to quickly make named routes for non-RESTful actions.
Ruby
39
star
28

searchify

Rails plugin to add extra searching functionality to models.
Ruby
37
star
29

selenium-on-rails

This repo is no longer maintained, see the official repository by paytonrules.
JavaScript
34
star
30

ryan-on-rails.tmbundle

Some TextMate snippets I use when working with Ruby and Rails.
26
star
31

dailystamp

Source code for my Rails Rumble 2009 submission
Ruby
23
star
32

url_formatter

Format and validate a URL in Active Record. Example gem for RailsCasts.
Ruby
18
star
33

association-freezer

Freeze a belongs_to association in Active Record.
Ruby
17
star
34

admiteer

Rails Rumble 2007 project by Jack Canty, Kelli Shaver, and Ryan Bates
17
star
35

todo-list.tmbundle

A simple TextMate bundle to manage a todo lists.
14
star
36

myideadrawer

Rails Rumble 2008 entry by Ryan Bates and Kelli Shaver
Ruby
13
star
37

advent-2022

Advent of Code in Elixir
Elixir
12
star
38

blog-screencast

Example blog application built in the offical 15 minute Rails screencast.
Ruby
12
star
39

xapit-sync

Rails plugin to automatically reload a Xapian database when models change.
Ruby
12
star
40

ryan-bates.tmbundle

Miscellaneous commands and snippets I use in TextMate.
11
star
41

maestro

Piano exercise game written in MacRuby.
Ruby
11
star
42

vscode-railscasts-theme

RailsCasts Theme for VS Code
7
star
43

ryanb.github.io

Personal site for Ryan Bates
5
star
44

vscode-erb-syntax

ERB Syntax for VS Code
5
star
45

xapit-server

Rack server for interacting with a Xapian database remotely through Xapit.
Ruby
4
star
46

swapper

Ruby script for swapping two elements on a line (to be used in text editors).
3
star
47

bookmarklets

JavaScript
1
star
48

wallaby-rails-7-1-2

Example Rails 7.1.2 app with Wallaby
Ruby
1
star