Answer the question
In order to leave comments, you need to log in
Socket.io: how to get a specific socket by id?
Good afternoon.
There is the following situation.
From server.js I forward to another file io = require('socket.io')(server);
A request comes from another server with a cookie containing the id of the desired socket.
The goal is to get the socket object knowing its id, because you need to edit his session.
Getting through io.sockets.sockets[socketId] and through io.sockets.socket(socketId) fails.
Answer the question
In order to leave comments, you need to log in
sockets.io 1.0.6
I got it like this.
if (io && io.sockets && io.sockets.sockets) {
var sockets = io.sockets.sockets,
socket = null;
if (sockets.length) {
for (var i = 0; i < sockets.length; i++) {
var _socket = sockets[i];
if (_socket.id && _socket.id === socketId) {
socket = _socket;
break;
}
}
}
return socket;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question