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

269 views Asked by At

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.

1

There are 1 answers

0
Jerry C. On BEST ANSWER

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.