- Node.js like I/O plugin for Sinatra.
- Automatically selects from Comet and WebSocket.
- https://github.com/shokai/sinatra-rocketio
- Wiki
- Handle 10K+ clients on 1 process
% gem install sinatra-rocketio
- Ruby 1.8.7 or 1.9.2 or 1.9.3 or 2.0.0
- Sinatra 1.3.0+
- EventMachine
- jQuery
Server Side
require 'sinatra'
require 'sinatra/rocketio'
set :cometio, :timeout => 120, :post_interval => 2, :allow_crossdomain => false
set :websocketio, :port => 5001
set :rocketio, :websocket => true, :comet => true # enable WebSocket and Comet
run Sinatra::Application
io = Sinatra::RocketIO
io.on :connect do |client|
puts "new client available - <#{client.session}> type:#{client.type} from:#{client.address}"
io.push :temperature, 35 # to all clients
io.push :light, {:value => 150}, {:to => client.session} # to specific client
end
Client Side
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="<%= rocketio_js %>"></script>
var io = new RocketIO().connect();
io.on("temperature", function(value){
console.log("server temperature : " + value);
}); // => "server temperature : 35"
io.on("light", function(data){
console.log("server light sensor : " + data.value);
}); // => "server light sensor : 150"
Client Side
io.on("connect", function(){
io.push("chat", {name: "shokai", message: "hello"}); // client -> server
});
Server Side
io.on :chat do |data, client|
puts "#{data['name']} : #{data['message']} <#{client.session}> type:#{client.type} address:#{client.address}"
end
## => "shokai : hello <12abcde345f6g7h8ijk> type:websocket"
Client Side
io.on("connect", function(){
alert("connect!! "+io.session);
});
Server Side
io.on :connect do |client|
puts "new client <#{client.session}> type:#{client.type} address:#{client.address}"
io.push :hello, "hello new client!!"
end
io.on :disconnect do |client|
puts "client disconnected <#{client.session}> type:#{client.type}"
end
Client Side
io.on("error", function(err){
console.error(err);
});
Server Side
event_id = io.on :chat do |data, client|
puts "chat #{data} - from:#{client.session} type:#{client.type}"
end
io.removeListener event_id
or
io.removeListener :chat # remove all "chat" listener
Client Side
var event_id = io.on("error", function(err){
console.error("RocketIO error : "err);
});
io.removeListener(event_id);
or
io.removeListener("error"); // remove all "error" listener
make client groups.
Client Side
var io = new RocketIO({channel: "ch1"}).connect(); // set channel "ch1"
io.on("connect", function(){
io.push("hi", "haaaaaaaai!!");
});
io.on("announce", function(msg){
alert(msg);
// => alert "new client a1b2cde345fg in ch1"
});
Server Side
io = Sinatra::RocketIO
io.on :connect do |client|
msg = "new client #{client.session} in #{client.channel}"
io.push :announce, msg, :channel => client.channel # to all clients in Channel "ch1"
end
io.on :hi do |msg, client|
puts "client says #{msg} (channel:#{client.channel})"
# => "client says haaaaaaaai!! (channel:ch1)"
end
configure with ENV variables.
% export WS_PORT=9000
% export WEBSOCKET=disable
% export COMET=enable
% bundle exec rackup config.ru #=> start sinatra app
disable comet and enable websocket on port 9000.
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="/rocketio.min.js"></script>
var io = new RocketIO().connect("http://example.com");
io.on("connect", function(){
alert(io.type + " connect!! " + io.session);
});
% npm install -g uglify-js
% gem install bundler
% bundle install
% rake jslib
=> rocketio.js and rocketio.min.js
% gem install bundler
% bundle install
start server
% rake test_server
run test
% rake test
- 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