0.0
No commit activity in last 3 years
No release in over 3 years
Comet component for Sinatra RocketIO
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
>= 0
>= 0

Runtime

>= 1.0.0
>= 1.7.0
>= 1.3.0
 Project Readme

sinatra-cometio

Comet component for Sinatra RocketIO

Installation

% gem install sinatra-cometio

Requirements

  • Ruby 1.8.7 or 1.9.2 or 1.9.3 or 2.0.0
  • Sinatra 1.3.0+
  • EventMachine
  • jQuery

Usage

Server --(Comet)--> Client

Server Side

require 'sinatra'
require 'sinatra/cometio'
set :cometio, :timeout => 120, :post_interval => 1, :allow_crossdomain => false

run Sinatra::Application
io = Sinatra::CometIO

io.push :temperature, 35  # to all clients
io.push :light, {:value => 150}, {:to => session_id} # to specific client

Client Side

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="<%= cometio_js %>"></script>
var io = new CometIO().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 --(Ajax)--> Server

Client Side

io.on("connect", function(){
  io.push("chat", {name: "shokai", message: "hello"}); // client -> server
});

Server Side

io.on :chat do |data, session|
  puts "#{data['name']} : #{data['message']}  <#{session}>"
end
## => "shokai : hello  <12abcde345f6g7h8ijk>"

On "connect" Event

Client Side

io.on("connect", function(session_id){
  alert("connect!! "+session_id);
});

Server Side

io.on :connect do |session|
  puts "new client <#{session}>"
  io.push :hello, "hello new client!!"
end

io.on :disconnect do |session|
  puts "client disconnected <#{session}>"
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, from|
  puts "#{data} - from#{from}"
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("CometIO error : "err);
});
io.removeListener(event_id);

or

io.removeListener("error");  // remove all "error" listener

Sample App

chat app

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