Load balance websocket connection via dynamic url parameter

681 views Asked by At

In order to scale our real time application, we need to add horizontally web socket servers.

Each user connect via an url containing a groupId variable: http://ws.example.com/{groupId}. That groupId is dynamic and can't be forcasted.

Thus, many users share the same groupId. In order to communicate between them through the websocket server, each user sharing the same groupId has to be in the same websocket server.

We want to implement an load balancing system to balance between our websocket servers.

-If a client (userA) connect and another connection with the same groupId exist (userB) on the serverA, then we want to connect the userA on the serverA.

-If no existing connection with that groupId exists, then we allow a websocket server via classic load balancing algorithms.

How can we accomplish this ? Our servers are currently running on nginx.

1

There are 1 answers

0
vtortola On

Websocket uses HTTP negotiation, but it does not support redirection (301 or 302). So once your user have connected, you have to check if it is in the right place, otherwise send a message indicating that it must connect somewhere else.

So you would have serverA.whatever.com, serverB.whatever.com and ws.whatever.com balancing between those two. Then if a user connects to ws://ws.whatever.com and it is "balanced" to serverA.whatever.com, but because the parameter in the url it should connect to the other, you will have to implement some logic, so the server says { messageType:"connection", message:"reconnect", location:"ws://serverB.whatever.com"}, and the client understands the message and tries to reconnect to the such "location".

It is a pain, because this means the hosts must be aware of their own hostnames.