Send a message to a specific user in nodejs socket.io

1

Greetings, how could I make a message only reach a specific user, because using io.sockets.emit makes the message reach everyone but I could not make it reach only a user that selects, if They could guide me, I'd appreciate it.

    
asked by Teresa 08.09.2017 в 01:22
source

1 answer

2

you can do it in the following way:

io.to(socketid).emit('message', 'Este mensaje es único para un usuario');

The socketid is the identifier of each of the clients connected to your server (Keep in mind that the same user can open 3 browser windows, and this would be 3 different clients).

You can, for example, create an array of socketid's and even relate the socketid of the connection with that of a logged user of your application if necessary. In this way you could distinguish it and know who it is at any time.

I hope it helps you!

    
answered by 12.09.2017 в 15:25