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]
}