Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO 1.0.x: Get socket by id

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?

like image 474
Dũng Nguyễn Avatar asked Nov 21 '14 07:11

Dũng Nguyễn


4 Answers

For socket.io 1.0 use:

io.sockets.connected[socketId]

For 0.9 its io.sockets.sockets[socketId] and not io.sockets.socket[socketId]

like image 106
Sarita Avatar answered Oct 19 '22 10:10

Sarita


you can also use like:

io.to(socketid).emit();
like image 43
himanshu yadav Avatar answered Oct 19 '22 10:10

himanshu yadav


Socket.io Version 4.0.0

io.sockets.sockets.get(socketId);

like image 18
Sanjay Nishad Avatar answered Oct 19 '22 11:10

Sanjay Nishad


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().

like image 9
isaacsan 123 Avatar answered Oct 19 '22 10:10

isaacsan 123