0

I have a small chat application. I want to send a message to only the newest or last user to join the chatroom. when I use:

socket.broadcast.emit('chat message');

This (broadcast) does the opposite of what I would like to accomplish. Is there a different built-in function that I can use instead of broadcast? I also tried emit without the broadcast and that sends to everyone. I would appreciate any help. Thansk.

user1362262
  • 49
  • 1
  • 7
  • You can create a `room` and `broadcast` the message to users which they are not in room for more information read this part of [socket.io](http://socket.io/docs/server-api/#socket#in(room:string):socket) – Heartbit Dec 30 '16 at 21:11
  • 2
    `socket.emit()` sends only to that particular socket. That's how you send to one socket. Get the right `socket` object and call `.emit()` on it. – jfriend00 Dec 30 '16 at 21:24

1 Answers1

0

socket.emit() sends only to that particular socket. That's how you send to one socket.

So, get the desired socket object and call socket.emit().

.broadcast(), on the other hand, sends to all the sockets if called in the io object or to all sockets but the current one if used with socket.broadcast.emit().

Here's a pretty good summary of the various options: https://stackoverflow.com/a/10099325/816620

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979