Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Makes it easy to upgrade any request to a websocket connection in Sinatra

branch: master

This branch is 0 commits ahead and 0 commits behind master

Octocat-spinner-32 examples run from cli April 03, 2013
Octocat-spinner-32 lib version bump April 03, 2013
Octocat-spinner-32 spec adds basic unit tests for error module April 03, 2013
Octocat-spinner-32 .gitignore bump version October 09, 2012
Octocat-spinner-32 version bump April 03, 2013
Octocat-spinner-32 LICENSE.txt renamed and restructured April 25, 2012
Octocat-spinner-32 typo fix November 02, 2013
Octocat-spinner-32 Rakefile Forgot to change this. October 09, 2011
Octocat-spinner-32 VERSION Version 0.2.0, hooray! October 09, 2011
Octocat-spinner-32 sinatra-websocket.gemspec refs #3 use em-websocket between 0.3.6 and 0.3.X February 20, 2013


Makes it easy to upgrade any request to a websocket connection.

SinatraWebsocket is a fork of Skinny merged with Rack WebSocket. It provides helpers methods to detect if a request is a WebSocket request and defer to an EM::WebSocket::Connection.

Put this in your pipe ...

require 'sinatra'
require 'sinatra-websocket'

set :server, 'thin'
set :sockets, []

get '/' do
  if !request.websocket?
    erb :index
    request.websocket do |ws|
      ws.onopen do
        ws.send("Hello World!")
        settings.sockets << ws
      ws.onmessage do |msg|
        EM.next_tick { settings.sockets.each{|s| s.send(msg) } }
      ws.onclose do
        warn("websocket closed")

@@ index
     <h1>Simple Echo & Chat Server</h1>
     <form id="form">
       <input type="text" id="input" value="send a message"></input>
     <div id="msgs"></div>

  <script type="text/javascript">
    window.onload = function(){
        var show = function(el){
          return function(msg){ el.innerHTML = msg + '<br />' + el.innerHTML; }

        var ws       = new WebSocket('ws://' + + window.location.pathname);
        ws.onopen    = function()  { show('websocket opened'); };
        ws.onclose   = function()  { show('websocket closed'); }
        ws.onmessage = function(m) { show('websocket message: ' +; };

        var sender = function(f){
          var input     = document.getElementById('input');
          input.onclick = function(){ input.value = "" };
          f.onsubmit    = function(){
            input.value = "send a message";
            return false;

And Smoke It

ruby echo.rb


Copyright (c) 2012 Caleb Crane.

Portions of this software are Copyright (c) Bernard Potocki and Samuel Cochran.

See License.txt for more details.

Something went wrong with that request. Please try again.