I have a Ruby application running with Puma. Now I wanted to add a websocket to broadcast REST requests to the connected clients. I managed to create a websocket server with em-websocket gem just by adding some code in my config.ru:
require_relative 'config/environment'
require 'em-websocket'
Socket = EM.run {
@channel = EM::Channel.new
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 2929, :debug => true) do |ws|
ws.onopen {
sid = @channel.subscribe { |msg| ws.send msg }
@channel.push "#{sid} connected!"
ws.onmessage { |msg|
@channel.push "blubb"
}
ws.onclose {
@channel.unsubscribe(sid)
}
}
end
}
run Rails.application
The problem now is that when I run 'rails server -b 0.0.0.0' the websocket is running but my application is not. When connecting to localhost:3000 I get a time out. What can I do to run them together?
Follow up question: How can I broadcast over the websocket to the connected clients? I thought about adding to the controller of the REST requests a broadcast method. Is that possible?