Sunday, 4 June 2017

Multiple websockets onto multiple servers: how do they communicate?

I have a node server accepting websocket connections from the clients. Each client can broadcast a message to all of the other clients.

At the moment, the server has an array with all of the connections. Each connection has a tabId. When one of the client emits a message, I go through all of the connections and check: if the connection's tabId doesn't match, I send the message to the client.

For loading issues, I am facing the problem of having to have more than one server. So, there will be say two servers, each one with a number of clients.

How do I make sure that a message gets broadcast to all of the websocket clients, and not only the ones connected to the same server?

One possible solution I thought is to have the connections stored on a database, where each record has the tabId and the serverId. However, even a simple broadcast gets tricky as messages to "local" sockets are easy to broadcast (the socket is local and available) whereas messages to "remote" sockets are tricky, and would imply intra-server communication.

Is there a good pattern to solve this? Surely, this is something that people face every day.



via Merc

No comments:

Post a Comment