It doesn't really matter where in your project you store it, but my hunch is that this will end up function much like a controller class in Rails.
A basic Rack middleware for this looks like:
class WebSocket
def initialize(app)
@app = app
end
def call(env)
if Faye::WebSocket.websocket?(env)
# handle websocket connection
else
@app.call(env)
end
end
end
You'd then require this file, wherever it is, in your
config.ru file and put in front of the Rails stack:
use WebSocket
In the middleware above, where I've put 'handle websocket connection', you'd do something like this:
socket = Faye::WebSocket.new(env)
socket.onmessage = lambda |event|
# event.data is the message text
end
socket.onclose = lambda |event|
socket = nil
end
Then it's up to you to do whatever you want with incoming messages. You can use your Rails models or any other internal code here.