Home

Awesome

WebSocket Server for Ruby

WebSocket-EventMachine-Server is Ruby WebSocket server based on EventMachine.

Why another WebSocket server?

There are multiple Ruby WebSocket servers, each with different quirks and errors. Most commonly used em-websocket is unfortunately slow and have multiple bugs(see Autobahn tests above). This library was created to fix most of them.

Installation

gem install websocket-eventmachine-server

or in Gemfile

gem 'websocket-eventmachine-server'

Simple server example

EM.run do

  WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => 8080) do |ws|
    ws.onopen do
      puts "Client connected"
    end

    ws.onmessage do |msg, type|
      puts "Received message: #{msg}"
      ws.send msg, :type => type
    end

    ws.onclose do
      puts "Client disconnected"
    end
  end

end

Options

Following options can be passed to WebSocket::EventMachine::Server initializer:

Methods

Following methods are available for WebSocket::EventMachine::Server object:

onopen

Called after client is connected.

Parameters:

Example:

ws.onopen do |handshake|
  puts "Client connected with params #{handshake.query}"
end

onclose

Called after client closed connection.

Example:

ws.onclose do
  puts "Client disconnected"
end

onmessage

Called when server receive message.

Parameters:

Example:

ws.onmessage do |msg, type|
  puts "Received message: #{msg} or type: #{type}"
end

onerror

Called when server discovers error.

Parameters:

Example:

ws.onerror do |error|
  puts "Error occured: #{error}"
end

onping

Called when server receive ping request. Pong request is sent automatically.

Parameters:

Example:

ws.onping do |message|
  puts "Ping received: #{message}"
end

onpong

Called when server receive pong response.

Parameters:

Example:

ws.onpong do |message|
  puts "Pong received: #{message}"
end

send

Sends message to client.

Parameters:

Example:

ws.send "Hello Client!"
ws.send "binary data", :type => :binary

close

Closes connection and optionally send close frame to client.

Parameters:

Example:

ws.close

ping

Sends ping request.

Parameters:

Example:

ws.ping 'Hi'

pong

Sends pong request. Usually there should be no need to send this request, as pong responses are sent automatically by server.

Parameters:

Example:

ws.pong 'Hello'

Migrating from EM-WebSocket

This library is compatible with EM-WebSocket, so only thing you need to change is running server - you need to change from EM-WebSocket to WebSocket::EventMachine::Server in your application and everything will be working.

Using self-signed certificate

Read more here.

Support

If you like my work then consider supporting me:

Donate with Bitcoin

Donate with Ethereum

License

(The MIT License)

Copyright © 2012 Bernard Potocki

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.