require 'sinatra' require 'sinatra-websocket' require 'pressure' set :server, 'thin' set :sockets, [] data = {} Thread.new do loop do data = (0...8).map { (65 + rand(26)).chr }.join sleep(1.0 / 0.2) end end pressure = Pressure.new do data end pressure.wrapper_template = { someKey: 'Some Value', anotherKey: 'Another Value' } get '/' do if !request.websocket? erb :index else request.websocket do |ws| ws.onopen do pressure << ws puts "Connected: #{pressure.sockets.length}" end ws.onmessage do |msg| EM.next_tick { pressure.sockets.each { |socket| socket.send(msg) } } end ws.onclose do warn('websocket closed') pressure.delete ws end end end end __END__ @@ index

Pressure Demo