Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
WebSocket emulation - Ruby server
Ruby
tree: 8e1d677d78

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
examples/app
lib
spec
tests
.gitignore
.rspec
Gemfile
LICENCE
NOTES
README.textile
TODO.todo
WebsocketHttpErrors.test_invalidMethod.txt
faye-error.txt
sockjs.gemspec
unpack_tests.rb

README.textile

About

Disclaimer: This library is still work in progress.

SockJS is WebSocket emulation library. It means that you use the WebSocket API, only instead of WebSocket class you instantiate SockJS class. I highly recommend to read SockJS: WebSocket emulation on the RabbitMQ blog for more info.

Prerequisites

Even though SockJS uses Rack interfact, Thin is required as it supports polling. For Websockets, we use faye-websocket gem.

The Client-Side Part

For the client-side part you have to use JS library sockjs-client which provides WebSocket-like API. Here’s an example:

<script src="http://cdn.sockjs.org/sockjs-0.1.min.js"></script>

<script>
  var sock = new SockJS("http://mydomain.com/my_prefix");

  sock.onopen = function() {
    console.log("open");
  };

  sock.onmessage = function(e) {
    console.log("message", e.data);
  };

  sock.onclose = function() {
    console.log("close");
  };
</script>

The Server-Side Part

Now in order to have someone to talk to, we need to run a server. That’s exactly what is sockjs-ruby good for:

#!/usr/bin/env rackup -s thin
# encoding: utf-8

require "rack/sockjs"

use SockJS, "/echo" do |connection|
  connection.subscribe do |session, message|
    session.send(message)
  end
end

use SockJS, "/disabled_websocket_echo",
  disabled_transports: [SockJS::WebSocket] do |connection|
  # ...
end

use SockJS, "/close" do |connection|
  connection.session_open do |session|
    session.close(3000, "Go away!")
  end
end

run MyApp

For more complex example check examples/app/config.ru.

SockJS Family

Development

Get sockjs-protocol (installation information are in its README) and run config.ru from examples/app. Now you can run the tests against it, for instance:

# Run all the tests.
./venv/bin/python sockjs-protocol-0.1.py

# Run all the tests defined in XhrStreaming.
./venv/bin/python sockjs-protocol-0.1.py XhrStreaming

# Run only XhrStreaming.test_transport test.
./venv/bin/python sockjs-protocol-0.1.py XhrStreaming.test_transport

Links

Something went wrong with that request. Please try again.