2

Lets have an application that has real-time chat feature. The chat requires websockets in order to function real-time.

Now imagine we have "balancer" and 3 application servers. Instance of application run on every server.

The balancer delegates request to 1 of these 3 servers, for example based on traffic.

My question is, how to deal with websockets? Example:

1) User A logs into an applicition
2) Balancer delegates user A to server 1
3) Web socket is created for user A on server 1

4) User B logs into an applicition
5) Balancer delegates user B to server 3
6) Web socket is created for user B on server 3

7) User A wants to set message to user B
8) Instance of application on server 1 looks for web sockes for user B

Web sockets for user B exists on server 3, not on server 1.

How to deal with situation like this? Should it be treated on application level ( e.g some sync of app instances throught database/messages) or some configuration on "balancer" ?

Thanks for help.

  • Presumably, all of the application servers on a given load balancer perform similar functions. Why would which application server you are attached to with a websocket be an issue? Is it your intention to connect clients to each other through websockets directly? If so, why would the servers have to take part in that conversation? – Robert Harvey Aug 15 '18 at 16:40
6

Unless you know beforehand who wants to communicate with whom, you will not be able to solve this on the balancer level. If the user has to e.g. choose a chatroom prior to connecting, you could assign a fixed server to each conversation (calculate it based on a deterministic function taking the chatroom ID/name as parameter, giving a server instance as result).

Otherwise, interconnect all application servers in a publish-subscribe pattern. If User A sends a message to Server 1 for User B, but this user is not connected to the server, it should relay the message to the pub-sub network with topic User B. If another server, such as Server 3 is connected to User B, it is subscribed to the topic User B and can push the message to this user.

This is basically what happens:

  1. When a user connects to an application server, the application server starts to subscribe to a topic that is dedicated to this user.
  2. The user transmits a message to the server for a specific recipient.
  3. IF the server is already connected to the recipient, it just pushes the message.
  4. ELSE the server publishes the message to the dedicated topic for this user.
  5. When a server receives a message over the pub-sub network, it pushes the message to the connected user on the other server's behalf.
  6. When a user disconnects from an application server, the server unsubscribes from the topic.
  • thanks i will look deeper into publish-subscriber pattern – Darlyn Aug 20 '18 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.