A simple Ruby framework to build awesome WebSocket applications. The framework is based on the faye/websocket library.


You can see the chat application demo (available in examples/ folder) in action here:


require 'socker'

class TimeServer < Socker::App

  def initialize
    on :open, method(:connected)
    on :close, method(:disconnect)
      when_active: method(:start_timeserver),
      when_idle: method(:stop_timeserver)

  def connected(socket, event)
    log "Yay! Somebody just connected, lets start sending him current time."

  def disconnect(socket, event)
    log "Somebody has left :-("

  def start_timeserver
    @timeserver = EM.add_periodic_timer(1) { broadcast( }

  def stop_timeserver


You can use any websockets enabled server (like puma) to deploy this application.

You also need HTML file with the JavaScript code (check examples/ directory), that will speak to this server.

The Socker::App will then spawn a new EM timer and broadcast the current time to all connected users. After the last user disconnect, the EM timer will be killed (and restarted again when someone connect again).


It is Ruby, so:

$ gem install socker

or add Socker to your Gemfile:

source ''

gem 'socker'

How to run it?

As Socker is Rack compatible, you can use to spawn the WebSocket server. Your app can be also mounted to any Sinatra/Rails/Rack-compatible app and provide the WebSocket layer together with the regular HTTP server.

A simple example of

require 'puma'
load './timeserver.rb'


# This will make public/* files available in browser:
use Rack::Static, :urls => ["/images", "/js", "/css", "/index.html"], :root => "public"

# Mount the TimeServer application to '/' URI.
run'/' =>


Copyright 2013 Michal Fojtik

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.