Cool.io-WebSocket

Cool.io-WebSocket is a WebSocket server implementation based on Cool.io, a high performance event-driven I/O library for Ruby.

This library conforms to WebSocket draft-75 and draft-76.

Installation

gem install cool.io-websocket

Examples

  • examples/echo.rb - receives a message from clients and just echos.

  • examples/shoutchat.rb - simple chat application.

  • examples/rpc.rb - pushes messages to browsers from programs separated from the web socket server.

See examples/README.md for details.

Simple echo server

require 'rubygems'
require 'cool.io-websocket'

class MyConnection < Cool.io::WebSocket
  def on_open
    puts "WebSocket opened"
    send_message("Hello, world!")
  end

  def on_message(data)
    puts "WebSocket data received: '#{data}'"
    send_message("echo: #{data}")
  end

  def on_close
    puts "WebSocket closed"
  end
end

host = '0.0.0.0'
port = '8080'

server = Cool.io::WebSocketServer.new(host, port, MyConnection)
server.attach(Cool.io::Loop.default)

Cool.io::Loop.default.run

Publisher/Subscriber-style message routing

require 'rubygems'
require 'cool.io-websocket'

class PubSub
  def initialize
    @subscriber = {}
    @seqid = 0
  end

  def subscribe(&block)
    sid = @seqid += 1
    @subscriber[sid] = block
    return sid
  end

  def unsubscribe(key)
    @subscriber.delete(key)
  end

  def publish(data)
    @subscriber.each_value {|block|
      block.call(data)
    }
  end
end

$pubsub = PubSub.new

class MyConnection < Cool.io::WebSocket
  def on_open
    puts "WebSocket opened"
    @sid = $pubsub.subscribe {|data|
      send_message data
    }
  end

  def on_message(data)
    puts "WebSocket data received, broadcasting: '#{data}'"
    $pubsub.publish(data)
  end

  def on_close
    puts "WebSocket closed"
    $pubsub.unsubscribe(@sid)
  end
end

host = '0.0.0.0'
port = ARGV[0] || 8080

server = Cool.io::WebSocketServer.new(host, port, MyConnection)
server.attach(Cool.io::Loop.default)

puts "start on #{host}:#{port}"

Cool.io::Loop.default.run

Learn more

License

Copyright © 2010 FURUHASHI Sadayuki

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.