faye_shards

Comments

This gem will be helpful if you are planning to run more than one instance of Faye (doesn’t matter on 1 server or on many). If users in your application are subscribed to unique channels (eg. “/#current_usercurrent_user.id”) - faye_shards will fit all your needs. If you have global channels (which you use widely) and many instances of Faye - this game is not a perfect solution for you, but it will work. It doesn’t matter if you use Redis as data storage or not.

Check out these posts/discussions about Faye sharding:

Installing

Simply add

gem 'faye_shards'

to your Gemfile

Configuration

All you need to configure gem is to create file faye.yml in config folder. Here is an example:

development:
  shards:
    -
      port: 42000
      host: 127.0.0.1
    -
      port: 42001
      host: 127.0.0.1
production:
  shards:
    -
      local_host: 10.1.1.1
      port: 42000
      host: server1.myapp.com
      secured: true
      secured_port: 32000
    -
      local_host: 10.1.1.1
      port: 42001
      host: server1.myapp.com
      secured: true
      secured_port: 32001

Required options:

  • host - IP or domain name where instance of Faye is running

  • port - port, instance of Faye is listening to

Optional params:

  • local_host - local IP of server. Used for pushes from Rails instead of host if specified. Just to communicate via local interfaces.

  • secured - true/false. Specifies if there is a SSL Offloading configured for Faye instance.

  • secured_port - Required if secured is set to true.

So, in my example for production I have 2 Faye instances listening to ports 42000 and 42001 using HTTP + Stunnel configured to listen 32000 and 32001.

Usage

URL for Faye’s client-side JS (unless you store it somewhere on diff storage) is returned by this methos:

current_user.faye_shard.js_url(request.ssl?)

Module FayeShard::User::Faye which can be included to your User model provides method faye_channel which returns unique channel id for user. If you do not want to include module to your model - you need to manage unique channels for users yourself.

On client side, subscription should look like:

client = new Faye.Client(<%= raw current_user.faye_shard.url(request.ssl?).inspect %>);
fayeSubscription = client.subscribe(<%= raw current_user.faye_channel.inspect %>, function(data) {});

or

fayeSubscription = client.subscribe(<%= raw my_own_method_for_channel_id.inspect %>, function(data) {});

Pushing data to user’s channel also depends on your decision to include module FayeShard::User::Faye. So, 2 options:

current_user.push_to_faye(data) # Sends data to channel, returned by current_user.faye_channel

or

FayeShards.shard(current_user.id).push(my_own_method_for_channel_id, data)

Extensions can be passed as an additional hash after data

How to push to global channel and more information about gem can be found here: .

Roadmap / TODO

  • UT

  • Add example Rails app which uses faye_shards.

  • GOD scripts to start/monitor Faye instances based on faye.yml

Copyright © 2011 Alex Kazeko. See LICENSE.txt for further details.