4

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.

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54

4 Answers4

3

I believe what you want is along the lines of this:

 // send to all clients except sender
 socket.broadcast.emit('event', "this is a test");

 // send to all clients in 'room' room except sender
 socket.broadcast.to('room').emit('event', 'whadup');

  // sending to all clients in 'room' room, include sender
 io.sockets.in('room').emit('event', 'woodup');

You can add the "of.(namespace)" if you wish to specify the namespace as well.

Xinzz
  • 2,242
  • 1
  • 13
  • 26
  • No, I am not expecting this one, last line you said "You can add the "of.(namespace)" if you wish to specify the namespace as well". where to add the namespace and `broadcast`(sending the message to all user except the sender with namespace and room concept) not `emit` – Thirumalai murugan Dec 09 '13 at 04:24
  • What you said doesn't really make sense... this is the correct way to broadcast a message to everything in a certain room in a certain namespace with the message. If you are still confused view this: [link](http://stackoverflow.com/questions/6477770/socket-io-how-to-broadcast-messages-on-a-namespace?rq=1) or this [link](http://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender-socket-io). – Xinzz Dec 09 '13 at 15:16
  • 1
    How exactly do you use "of.(namespace)" ? Is there something like io.of('namespace').broadcast.to('room').emit() ? – Dejan Bogatinovski Apr 14 '15 at 22:33
1

Do this to send a message to everyone except the sender:

socket.broadcast.emit('event', "this is a test");
0

i have got that problem. io.of() confused me.

when you used 'connection' as io.of(),

your socket already got namespace. so you just add particular room.

io.of(namespace).in(room).emit('functionname', data); -> sokcet.broadcast.emit('event',data);

0

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

wei
  • 557
  • 1
  • 10
  • 24