-
Notifications
You must be signed in to change notification settings - Fork 486
How to retrieve socket(ws) from another nodes/pods #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You need to maintain the mapping between userId to socket Id const io = require('socket.io')(3000);
const redisAdapter = require('socket.io-redis');
io.adapter(redisAdapter({ host: 'localhost', port: 6379 }));
let socketId = '' // userId => socketId mapping
let client = io.sockets.connected[socketId] // if socket is connected to the same process
if (!client) { // if socket is probably connected in other node
client = io.to(socketId)
}
client.emit('msg', 'You can use client object to emit independently of its in the same node or another node') Go through this sheet https://socket.io/docs/emit-cheatsheet/ // sending to individual socketid (private message)
io.to(socketId).emit('hey', 'I just met you'); PS: I think this kind of questions get answered better in Stack Overflow. |
so i use
when i pass PS: i get this error only when socket is in other node thanks a lot for your help |
looks client returned by |
@Manjukb please see my answer there: #283 (comment) |
For future readers: please check https://socket.io/docs/v4/server-instance/#Utility-methods |
HI, is there anyway we can retrieve socket object across nodes/pods
need socket object(ws) to communicate with client but not able to get it
we are facing issue after scaling our nodejs to multiple pod
The text was updated successfully, but these errors were encountered: