In the 0.9.x version, we can get socket by ID like this:
io.sockets.socket(socketId)
But in 1.0.x we can't. How to find a socket by id in 1.0.x?
In the 0.9.x version, we can get socket by ID like this:
io.sockets.socket(socketId)
But in 1.0.x we can't. How to find a socket by id in 1.0.x?
For socket.io 1.0 use:
io.sockets.connected[socketId]
For 0.9 its io.sockets.sockets[socketId] and not io.sockets.socket[socketId]
io.sockets.connected[socketId].emit()
but it did work similarly var nsp = io.of('/my-namespace');
then nsp.connected[socketId].emit()
. Thanks
Sep 22, 2016 at 5:03
io.of('/namespace').connected[socketId].emit('message-tag', data, callback)
I think it is worth to mention that I can also pass a callback function as a bridge to request data from the connected client this way.
Version 3.0.3
// in "of", you can put '/' or whatever namespace you're using
io.of('/').sockets.get(socketId)
Basically, sockets
is no longer a simple Object. It's a Map, so you have to use .get()
.
Socket.io Version 2.0.3+
let namespace = null;
let ns = _io.of(namespace || "/");
let socket = ns.connected[socketId] // assuming you have id of the socket
The most simlple way I tried is:
var socket1 = io.of("/").connected[socketId];
Then you can do
socket1.join("some room");
socket1.leave("some room");
socket1.emit();
or other things you want.