Skip to content

shokai/sinatra-rocketio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sinatra-rocketio

Installation

% gem install sinatra-rocketio

Requirements

Usage

Server --(WebSocket/Comet)--> Client

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 --(WebSocket/Ajax)--> Server

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"

On "connect" Event

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

On "error" Event

Client Side

io.on("error", function(err){
  console.error(err);
});

Remove Event Listener

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

Chanel

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

Config with ENV[]

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.

Sample Apps

JavaScript Lib for browser

Download

Usage

<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);
});

Generate JS Lib

% npm install -g uglify-js
% gem install bundler
% bundle install
% rake jslib

=> rocketio.js and rocketio.min.js

Test

% gem install bundler
% bundle install

start server

% rake test_server

run test

% rake test

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

About

WebSocket/Comet plugin for Sinatra.

Resources

License

Stars

Watchers

Forks

Packages

No packages published