I have been using Sinatra for sometime now and I would like to add some realtime features to my web-app by pushing the data via websockets.
I have successfully used
I've been using sinatra-websocket. It let's you run the websocket server in the same process and on the same port as Sinatra.
Disclaimer: I'm the maintainer.
require 'sinatra'
require 'sinatra-websocket'
set :server, 'thin'
set :sockets, []
get '/' do
if !request.websocket?
erb :index
else
request.websocket do |ws|
ws.onopen do
ws.send("Hello World!")
settings.sockets << ws
end
ws.onmessage do |msg|
EM.next_tick { settings.sockets.each{|s| s.send(msg) } }
end
ws.onclose do
warn("websocket closed")
settings.sockets.delete(ws)
end
end
end
end
__END__
@@ index
Simple Echo & Chat Server