Answer the question
In order to leave comments, you need to log in
How to send notifications to a specific user in Node.js?
I found this code, but if everything is clear with sending a message to yourself and everyone except yourself, then where io.sockets.socket(socketid).emi
t is not clear at all. Where does io.sockets come from, what is socketid? I just found the answer to the question, and this code is supposedly from the docks, but I did not find anything similar in the docks.
// отправить текущему сокету сформировавшему запрос (туда откуда пришла)
socket.emit('message', "this is a test");
// отправить всем пользователям, включая отправителя
io.sockets.emit('message', "this is a test");
// отправить всем, кроме отправителя
socket.broadcast.emit('message', "this is a test");
// отправить всем клиентам в комнате (канале) 'game', кроме отправителя
socket.broadcast.to('game').emit('message', 'nice game');
// отправить всем клиентам в комнате (канале) 'game', включая отправителя
io.sockets.in('game').emit('message', 'cool game');
// отправить конкретному сокету, по socketid
io.sockets.socket(socketid).emit('message', 'for your eyes only');
Answer the question
In order to leave comments, you need to log in
When you catch a connection event for a user, add him to the "room" with his id. Thus, even if the user logged in from different devices, then his socket.id will be in the same "room" and you will be able to emit an event just by the user's id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question