Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sockjs/sockjs-ruby
WebSocket emulation - Ruby server
https://github.com/sockjs/sockjs-ruby
Last synced: about 1 month ago
JSON representation
WebSocket emulation - Ruby server
- Host: GitHub
- URL: https://github.com/sockjs/sockjs-ruby
- Owner: sockjs
- License: mit
- Created: 2011-09-28T12:59:31.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2012-07-20T15:14:51.000Z (over 12 years ago)
- Last Synced: 2024-04-15T14:09:04.070Z (9 months ago)
- Language: Ruby
- Homepage: http://sockjs.org
- Size: 1.04 MB
- Stars: 60
- Watchers: 6
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.textile
Awesome Lists containing this project
README
h1. 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":http://www.rabbitmq.com/blog/2011/09/13/sockjs-websocket-emulation on the RabbitMQ blog for more info.
h2. Prerequisites
Even though this library uses Rack interface, *Thin is required* as "it supports asynchronous callback":http://macournoyer.com/blog/2009/06/04/pusher-and-async-with-thin. For Websockets, we use "faye-websocket":http://blog.jcoglan.com/2011/11/28/announcing-faye-websocket-a-standards-compliant-websocket-library gem.
h2. The Client-Side Part
For the client-side part you have to use JS library "sockjs-client":http://sockjs.github.com/sockjs-client which provides WebSocket-like API. Here's an example:
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");
};h2. 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 ruby
# encoding: utf-8require "rack"
require "rack/sockjs"
require "eventmachine"# Your custom app.
class MyHelloWorld
def call(env)
body = "This is the app, not SockJS."
headers = {
"Content-Type" => "text/plain; charset=UTF-8",
"Content-Length" => body.bytesize.to_s
}[200, headers, [body]]
end
endapp = Rack::Builder.new do
# Run one SockJS app on /echo.
use SockJS, "/echo" do |connection|
connection.subscribe do |session, message|
session.send(message)
end
end# ... and the other one on /close.
use SockJS, "/close" do |connection|
connection.session_open do |session|
session.close(3000, "Go away!")
end
end# This app will run on other URLs than /echo and /close,
# as these has already been assigned to SockJS.
run MyHelloWorld.new
endEM.run do
thin = Rack::Handler.get("thin")
thin.run(app.to_app, Port: 8081)
endFor more complex example check "examples/sockjs_apps_for_sockjs_protocol_tests.rb":https://github.com/sockjs/sockjs-ruby/blob/master/examples/sockjs_apps_for_sockjs_protocol_tests.rb
h2. SockJS Family
* "SockJS-client":https://github.com/sockjs/sockjs-client JavaScript client library.
* "SockJS-node":https://github.com/sockjs/sockjs-node Node.js server.
* "SockJS-ruby":https://github.com/sockjs/sockjs-ruby Ruby server.
* "SockJS-protocol":https://github.com/sockjs/sockjs-protocol protocol tests and documentation.
* "SockJS-protocol spec":http://sockjs.github.com/sockjs-protocol/sockjs-protocol-0.2.1.htmlh1. Development
Get "sockjs-protocol":https://github.com/sockjs/sockjs-protocol (installation information are in its README) and run @./examples/sockjs_apps_for_sockjs_protocol_tests.rb@. Now you can run the tests against it, for instance:
# Run all the tests.
./venv/bin/python sockjs-protocol-0.2.1.py# Run all the tests defined in XhrStreaming.
./venv/bin/python sockjs-protocol-0.2.1.py XhrStreaming# Run only XhrStreaming.test_transport test.
./venv/bin/python sockjs-protocol-0.2.1.py XhrStreaming.test_transporth1. Links
* "SockJS: WebSocket emulation":http://www.rabbitmq.com/blog/2011/09/13/sockjs-websocket-emulation
* "SockJS: web messaging ain't easy":http://www.rabbitmq.com/blog/2011/08/22/sockjs-web-messaging-aint-easy
* "PubSubHuddle Realtime Web talk":http://www.rabbitmq.com/blog/2011/09/26/pubsubhuddle-realtime-web-talk