I would recommend using a Websockets framework, such as the Plezi web-app framework, which would handle both the websocket connection IO and help you broadcast data* across multiple processes through a Redis server.
(* notice that although Redis can store data, it cannot store objects and it definitely cannot store live sockets)
For example: Plezi would let you fork your server and it will automatically use Redis to broadcast/unicast data between the websockets on different processes or on different machines:
require 'plezi'
ENV['PL_REDIS_URL'] = "redis://username:password@my.host:6379"
# fork to 4 processes, each will have at least 2 Redis connections
GR::Settings.set_forking 4
# ... plezi code
Plezi.start # now it will fork.
A working chat server using the Plezi framework would look something like this*:
* this is just a draft to run in the irb
terminal. A Plezi app set up with the plezi new app_name
command would look way more organized.
require 'plezi'
# # optional Redis URL: automatic broadcasting across processes:
# ENV['PL_REDIS_URL'] = "redis://username:password@my.host:6379"
class ChatController
def index
%q{ This is a Plezi chat demo app using websockets.
To test this app, go to: http://www.websocket.org/echo.html
In the Location URL fill in the following url: ws://localhost:3000/nickname
Click Connect. No the app will act as a chat demo server with your browser as the client.
Try running the demo from two different browser windows to see chat messages between windows.
remember to set the correct Location URL in each window.}
end
def on_message data
msg = "#{params[:id] ? params[:id].to_s : 'unknown'}: #{data}"
broadcast :_send, msg
_send msg
true
end
def _send message
response << message
end
end
# starts listening with default settings, on port 3000
listen
# this is automatically converted to the RESTful route: '/(:id)'
route '/', ChatController
# exit terminal to start server
exit