2

So I have a chat room, and I want to emit a message to everyone except me (b/c I'm going to update the chat window via JS on form submit with my message), I tried like this:

io.sockets.in(socket.room)
          .broadcast
          .emit('new message', {
            'username' : socket.nickname,
            'message' : data,
            'color' : socket.color
          });

It doesn't work. Do you guys know how can I do this?

mscdex
  • 104,356
  • 15
  • 192
  • 153
Uffo
  • 9,628
  • 24
  • 90
  • 154
  • 1
    Should probably be `socket.broadcast.to(socket.room).emit(...);` by the nice list in this answer: http://stackoverflow.com/a/10099325/4879 – pawel Jan 21 '15 at 20:47

1 Answers1

-3

If you just remove the broadcast, you should sent to everyone, even the sender. The broadcast namespace makes so the message is sent to everyone except the sender. Just check the docs. So for example

io.sockets.in(socket.room)
      .emit('new message', {
        'username' : socket.nickname,
        'message' : data,
        'color' : socket.color
      });
mashumafi
  • 5
  • 4
  • 2
    Yeah but this emits to myself as well, i don't want to see the message...only the other ones that are in the room should see my message – Uffo Jan 21 '15 at 20:50