Nodjs How to broadcast message to namespace with room

Viewed 1355

I am able to send the message to particular namespace room with the following code

io.of(namespace).in(room).emit('functionname', data);

but it sending to all the connected client including the sender, what I want is I want to send the message excluding the sender, I have tried with following method but everything failed since syntax is not correct

io.broadcast.of(namespace).in(room).emit('functionname', data);

io.of(namespace).broadcast.in(room).emit('functionname', data);

io.of(namespace).in(room).broadcast.emit('functionname', data);

io.of(namespace).in(room).broadcast('functionname', data);

How can I send the message to every client excluding the sender.

4 Answers

I was having trouble with this too. The key is to use socket and not io

This will NOT work:

io.of(namespace).broadcast.to(room).emit('event', data)

However, this will work:

socket.to(room).emit('event', data)

One drawback though is that this will only work if the socket you are sending from is in the namespace you want to send to. If you want to send to a different namespace, I don't think this approach would work

Related