rubywebsocketeventmachineem-websocket

Is it safe to store an eventmachine websocket connection on the class level?


I'm writing a chat server application where the users can exchange messages with one of his friends. When the user connects i store his connection on an class variable hash:

@@connections[:user_id] = conn

When someone sends the user a message, I look for his connection on the hash and send the message through it. Sometimes the connection whith the clients simply dies, and the onclose callback is not invoked.


Solution

  • That works fine, and it's what I did when I started writing a web IRC client, but the problem is you're coupling your connected users to a single ruby process. If you wanted to spin up a 2nd em reactor, that 2nd process would not share the same class variables. You can get around that by using haproxy to split users between the different processes, but it's something to watch out for.