3

Lets say that there are 3 instances of application, aplication on one end is accepting web socket connections and on another end it is connected to RabbitMq as an consumer.

Each time when new web socket connection is created, socket is saved in JS object and mapped to customerId which is available from initial HTTP handshake request when app is about to switch protocol from HTTP to WS.

const socketStore = {}
io.on('connect', socket => {
  ...
  socketStore[customerId] = socket
})

On another end, every RabbitMQ message will have customerId in it as well which will be used to find appropriate socket object.

onNewMessage = message => {
  /**
   * Consumer parse the message and take customerId
   * and find a socket based on it
   */
  socketStore[customerId].emit(...)
}

The tricky part is that how to handle situation in which RabbitMQ consumer from whatever application instance will receive the message with customerId which cant be found in socketStore? So for the example, instance number 1 received web socket connection with customerId=10, but instance number 2 gets RabbitMQ message with that customerId. In that case instance number 2 cant find associated socket.

Additionally, single customer can be logged in with different devices at same time(desktop, mobile, ipad, ...), as it seems to me socketStore (where customerId is a key) should have one or more socket object per device, such as:

const socketStore = {
  '10': [socket, socket, ..., socket]
}
Srle
  • 10,366
  • 8
  • 34
  • 63
  • You should store that map in redis or other fast in memory storage (using single instance that will "synchronize" every participant. – Martin Adámek Oct 26 '17 at 21:23
  • what if all of your instances will subscribe to all messages? makes the architecture simpler. If the customer Id is not found in the socket store, instance will just do nothing. – Alex Buyny Oct 27 '17 at 00:42

1 Answers1

1

if scaling work loads, this keep one server to interact with customer, if he is online you can always find him. this structure shift the workloads to workers.

scaling work loads

scaling work loads

if scaling connections, if one server cannot hold all connections then you need a relay server to track user in a higher level scaling connections

also you can use both

Josh Lin
  • 2,397
  • 11
  • 21
  • Hey I configured an rmq setup using node amqp in a websocket, but I'm having problems with receiving the messages that I publish to my exchanges. Can you please have a look at my post here https://stackoverflow.com/questions/70320526/aws-api-gateway-websocket-receives-messages-inconsistently, thanks! – Uche Ozoemena Dec 13 '21 at 15:44