T
T
Timur Yakhin2014-04-17 15:34:36
Node.js
Timur Yakhin, 2014-04-17 15:34:36

How to find out the recipient id when sending via socket.broadcast?

How to find out/get/specify the client id when sending him a message using the socket.broadcast method?
An example of code in which the server forwards an incoming message to other clients.

socket.on('set_data', function (data) {		// обработчик входящего сообщения
  socket.broadcast.emit('set_data', {
    from: socket.id,		// от этого клиента пришло сообщение
    to: ?,				// этому клиенту пересылаем сообщение
    data: data
  });
});

Thanks in advance.
UPD I will clarify that the message should be sent to everyone, and Everyone should send His own id on the server.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Timur Yakhin, 2014-04-22
@temoffey

Well, as I understand it, this does not fit into the logic of socket.broadcast

A
axeax, 2014-04-18
@axeax

broadcast means sending a message to ALL. If I need to send to a specific socket, I write this: io.sockets.socket("required socket.id here").json.send(data); I work with json for this. In your case, I think it will be like this: io.sockets.socket("required socket.id here").emit(...)
UPD: I just saw your UPD =) Why not send users their id when they first connect?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question