Skip to content

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

Closed
Manjukb opened this issue Aug 26, 2020 · 5 comments
Closed

How to retrieve socket(ws) from another nodes/pods #353

Manjukb opened this issue Aug 26, 2020 · 5 comments
Labels
question Further information is requested

Comments

@Manjukb
Copy link

Manjukb commented Aug 26, 2020

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

@arunp0
Copy link

arunp0 commented Aug 27, 2020

You need to maintain the mapping between userId to socket Id
Whenever you want to communicate to any user, with socket Id found from the user mapping
(You can use Redis to maintain this mapping)

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.

@Manjukb
Copy link
Author

Manjukb commented Aug 27, 2020

so i use socket.io-request to pull data from socket client through http
so in an express API i need to find a specific socket object and communicate with it
here is the code

var ioreq = require('socket.io-request');
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)
}
ioreq(client ).request(req.body.method, {body: req.body}).then(function(res) {
logger.info(res, 'success result ');
result.json(res);
});

when i pass client instance I get Callbacks are not supported when broadcasting error from socket.io-request lib i was thinking that socket object what i pass is wrong ?? any idea ?? am i doing some mistake here ?

PS: i get this error only when socket is in other node

thanks a lot for your help

@Manjukb
Copy link
Author

Manjukb commented Aug 27, 2020

looks client returned by client = io.to(socketId) is not valid??
I think issue is `client = io.to(socketId)`` i will get you a room (ie.. list of connection) but i except a single connection ?? how to pinpoint to a single connection ??

@darrachequesne
Copy link
Member

@Manjukb please see my answer there: #283 (comment)

@darrachequesne darrachequesne added the question Further information is requested label Sep 1, 2020
@darrachequesne
Copy link
Member

For future readers: please check https://socket.io/docs/v4/server-instance/#Utility-methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants