Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sinatra-websocketio

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinatra-websocketio

  • 0.4.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

sinatra-websocketio

WebSocket component for Sinatra RocketIO

Installation

% gem install sinatra-websocketio

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 --(WebSocket)--> Client

Server Side

require 'sinatra'
require 'sinatra/websocketio'
set :websocketio, :port => 9000

run Sinatra::Application
io = Sinatra::WebSocketIO

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.10.2/jquery.min.js"></script>
<script src="<%= websocketio_js %>"></script>
var io = new WebSocketIO().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)--> 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){
  alert("connect!!");
});

io.on("disconnect", function(){
  alert("disconnected");
});

Server Side

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

io.on :disconnect do |session|
  puts "client disconnected <#{session}>"
end

On "error" Event

Client Side

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

Server Side

io.on :error do |e|
  STDERR.puts e
end

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("WebSocketIO error : "err);
});
io.removeListener(event_id);

or

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

Config

set :websocketio, :port => 9000

or

% WS_PORT=9000 rackup config.ru

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

FAQs

Package last updated on 05 Oct 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc