Working with socket IO on clustered environment ( NodeJS clusters) #4983
-
I am operating on a clustered environment using NodeJS clusters , I am trying to send direct messages from a client in worker/ room 1 to a client in worker/room a. i will transmit the message with the data of the room of recipient and the socketID of recipient
b. eventually i figured out that somehow just by writing this code socket.to(${socketId}).emit("message", {text}); it would automatically search for the client with the given socketID globally ( idk how is this working if anyone could help ) still i would like to know if anyone could help me finding how is it able to globally search for the recipient how efficient is it |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
If you use the {
data: {
packet: {
data: ["hello", "world"]
},
opts: {
rooms: ["theSocketId"]
}
}
} Then each worker will loop through their own sockets and find the one that is in the room named |
Beta Was this translation helpful? Give feedback.
-
When using hash-based routing with |
Beta Was this translation helpful? Give feedback.
In that case, I guess you could store the relationship in the primary process, and then forward the message to the right worker here: https://github.com/socketio/socket.io-cluster-adapter/blob/56a53bceb9a0f3032c68e9a9ae63643423890cb1/lib/index.ts#L552-L579