0

I'm trying to use broadcast statement to send messages to specific user.
However, when I'm starting with self-sending messages using the following statement, it doesn't work. The client side cannot receive the message. So what's wrong with this statement?

io.on('connection', function(socket) {
  socket.on('message', function(msg) {
    socket.broadcast.to(socket.id).emit('message', msg);
  });
});

(purpose of this code: the client side send a piece of message named 'message', the server receive the message and send back to the same client)
neolicd
  • 1,009
  • 2
  • 11
  • 14

1 Answers1

0

socket.broadcast.to broadcasts to all sockets in the given room, except to the socket on which it was called while io.sockets.in broadcasts to all sockets in the given room. (cited from Daniel Baulig's answer to another question:)
Socket.io rooms difference between broadcast.to and sockets.in

Community
  • 1
  • 1
neolicd
  • 1,009
  • 2
  • 11
  • 14