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 %>
< 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.create(params[:message])
socket.broadcast(, :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
-
Fork it
-
Create your feature branch (‘git checkout -b my-new-feature`)
-
Commit your changes (‘git commit -am ’Add some feature’‘)
-
Push to the branch (‘git push origin my-new-feature`)
-
Create new Pull Request