Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emit to Socket IO socket.id

I am trying to emit to a particular socket ID:

socket(user[playID]).emit('correct', data);

But I'm getting:

TypeError: object is not a function

if I log out user[playID] I do get a valid socket ID.

Appreciated!

Here is my setup in case I'm missing something:.

// Tell Socket.io to pay attention
servio  = io.listen(server);

// Tell HTTP Server to begin listening for connections on port 3250
sock    = server.listen(3250);
like image 976
Matthew Starkey Avatar asked Dec 02 '22 18:12

Matthew Starkey


1 Answers

This should do it

servio.sockets.socket(id).emit('hello');

This answer covers the same/similar topic. In short, consider keeping a reference to the connected clients yourself and emit to them as desired, rather than relying on socket.io's internals, which could change.

like image 186
Jaz Lalli Avatar answered Dec 10 '22 13:12

Jaz Lalli