= sinatra-cometio
* http://shokai.github.com/sinatra-cometio
== INSTALL:
* gem install sinatra-cometio
== DESCRIPTION:
Node.js like Comet I/O plugin for Sinatra
== REQUIREMENTS:
* Ruby 1.8.7+ or 1.9.2+
* Sinatra 1.3.0+
* EventMachine
* jQuery
== SYNOPSIS:
=== Server --(Comet)--> Client
Server Side
require 'sinatra'
require 'sinatra/cometio'
CometIO.push :temperature, 35 # to all clients
CometIO.push :light, {:value => 150}, {:to => session_id} # to specific client
Client Side
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.push("chat", {name: "shokai", message: "hello"}); // client -> server
Server Side
CometIO.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){
alert("connect!!");
});
Server Side
CometIO.on :connect do |session|
puts "new client <#{session}>"
end
=== On "error" Event
Client Side
io.on("error", function(err){
console.error(err);
});
=== Remove Event Listener
Server Side
event_id = CometIO.on :chat do |data, from|
puts "#{data} - from#{from}"
end
CometIO.removeListener event_id
or
CometIO.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
- http://cometio-chat.herokuapp.com
- https://github.com/shokai/sinatra-cometio/tree/master/sample
== LICENSE:
(The MIT License)
Copyright (c) 2012 Sho Hashimoto
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.