SocketableRails

Tests coming soon.

Socketable-rails uses @DanKnox Websocket-Rails gem: github.com/DanKnox/websocket-rails.

Installation

Add to Gemfile

gem 'socketable-rails'

Then

$ cd rails/app/path
$ bundle install
$ rake socketable:install
$ thin start

Usage

Building a realtime chat:

message.rb

Normal model

class Message < ActiveRecord::Base
  attr_accessible :content
end

home/index.html.erb

You need to add a :websocket => true parameter to your form

<%= form_for @message, :websocket => true do |f| %>
  <%= f.text_area :content %>
  <button class="btn">Submit</button>
<% end %>
<div id="msgs">
</div>

messages_controller.rb

Create a function named websocket in your controller to handle the request

class MessagesController < ApplicationController
  def websocket(params, socket)
    message = Message.create(params[:message])
    socket.broadcast(message, :create_message)
  end
end

javascript

Bind the event you triggered in the controller

Socketable.dispatcher().bind('create_message', function(data) {
  $('#msgs').append($('<div></div>').html(data.content));
});

Contributing

  1. Fork it

  2. Create your feature branch (‘git checkout -b my-new-feature`)

  3. Commit your changes (‘git commit -am ’Add some feature’‘)

  4. Push to the branch (‘git push origin my-new-feature`)

  5. Create new Pull Request