Home » Javascript » Scaling application with RabbitMq and websockets

Scaling application with RabbitMq and websockets

Posted by: admin November 1, 2017 Leave a comment


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

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.