SocketableRails
Tests coming soon.
Socketable-rails uses @DanKnox Websocket-Rails gem: http://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 %> <% end %>
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) 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($('
').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