Z
Z
zozyla2019-07-01 22:05:04
Node.js
zozyla, 2019-07-01 22:05:04

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).emit 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');

Is there a solution that just allows you to send a private message by username, session id, or something else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Skibin, 2019-07-01
@zozyla

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 question

Ask a Question

731 491 924 answers to any question