Is it possible to serialize a WebSocket connection object and reuse it on another server?

Viewed 622

I wanted to setup load balancing for my website and so when you request to the websocket address it will then route your connection to a WebSocket server with the most resources available at the current request time. My website is a chat website and updating the chat for other users with WebSockets isn't very hard if it's all running on one server.

Let's say User one sends a message to TestUser.

User has a connection ID of 120 and TestUser has the ID of 174, Now the server will look through the connections that match the user and get the ID then get the connection and then send the response to TestUser.

But if both users are on two different servers and I store their connections on a database in a serialized form and then get them to a variable, Can I still use that connection?

Do the connections contain some kind of authentication to send a message as a server? If not what could I try. I do not want to replace my load balancer with a single server.

Thanks!

NOTE: I listed Java and PHP as I use both languages for WebSockets for their own reasons, as I develop for other people based on what language they prefer, Which in my case mainly it's Java and PHP

1 Answers

It's not possible. A connection is a shared understanding between two parties that communication, using some port and protocol, is happening and ongoing. If that understanding is not present, actions on one end (such as deserializing a socket object) doesn't create one - a new connection needs to be created, which again establishes such understanding. This means that a) a previously closed connection object is not useful anymore and b) connection object referring to existing, live connection is not useful on some other server than where the connection was establised on.

See more answers on Serializing socket thread.

Related