118

What's the difference between io.sockets.emit and socket.broadcast.emit? Is it only that broadcast emits to everyone BUT the socket that sends it?

It seems like they can be used interchangeably:

io.sockets.on('connection', function (socket) {
  //these should do the same thing  
  io.sockets.emit('this', { receivers: 'everyone'});

  socket.broadcast.emit('this', { receivers: 'everyone but socket'}); //emits to everyone but socket
  socket.emit('this', { receivers: 'socket'}); //emits to socket
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
swiecki
  • 3,483
  • 3
  • 23
  • 19

4 Answers4

203

io.sockets.emit will send to all the clients

socket.broadcast.emit will send the message to all the other clients except the newly created connection

This Socket.IO Wiki post will help everyone reading this question:

The recent cheatsheet can also be viewed here:

https://socket.io/docs/v4/emit-cheatsheet

user1063287
  • 10,265
  • 25
  • 122
  • 218
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • 29
    well that took 3 hours of trouble shooting to figure out. – Petrogad Sep 11 '14 at 20:40
  • I can verify this is indeed the behaviour, but I cannot see any documentation for socket.broadcast nor socket.broadcast.emit in the Github docs nor on [the Socket.io website](http://socket.io/docs/client-api/). Am I missing something? (and the wiki has gone) – scipilot Apr 26 '15 at 01:13
  • 1
    What's even "better" about this is that the example "chat" application makes almost exclusive use of `socket.broadcast.emit` while that API is never documented anywhere. – Avi Cherry Jun 24 '15 at 22:21
  • @Sobiaholic I still haven't encountered the obvious reason for not emitting the change to the current connection. Is it for when the client who triggered the event already had made an optimistic update in advance? – Nick Pineda Jan 21 '16 at 23:53
  • 3
    It's not "except the newly created connections", it should be "to all other clients except itself". You can have a newly created connection (a client joining the chat room) and you send a message using `socket.broadcast.emit` and they will receive it, but not you. – NiCk Newman Apr 19 '16 at 13:02
  • Thank you for this. I pulled out half my hair and gave up. – Noufal Ibrahim Jul 08 '21 at 13:38
52

socket.broadcast.emit() behaves similar to io.sockets.emit, but instead of emitting to all connected sockets it will emit to all connected socket except the one it is being called on. So in this case the socket referenced by socket will not receive the event.

softvar
  • 17,917
  • 12
  • 55
  • 76
Karthic Rao
  • 3,624
  • 8
  • 30
  • 44
  • 6
    one more update - as per Jayantha's answer and my own analysis it also DOESN'T emit to newly created connections i.e. the new client connections which were created after the 'socket' was created. This is an important and key difference !! – Anmol Saraf Sep 30 '13 at 21:35
  • 7
    The "newly created connection" Jayantha refers to is the socket whose connection event is being handled and who would issue the broadcast. The point of broadcast is that it goes to every client except the 'broadcasting' socket -- the order that the client connections were established makes no difference. – Semicolon Jan 01 '14 at 15:40
11

Scenario:1:- By the use of io.sockets.emit Detailed Diagram:-io.sockets.emit

Here Every Socket gets the Message including Initiator.

  // BY IO>SOCKETS>EMIT
   io.sockets.emit('MyChannelBroadcast',
               {
                 owner:"Anshu Ashish",
                 clientCount:clients,
                 message:"Welcome All"
               }
    );

Scenario:2:- By the use of socket.broadcast.emit Detailed Diagram:-socket.broadcast.emit

Here Every Sockets are getting Message Except One i.e Initiator.

    // BY SOCKET>BROADCAST>EMIT
   socket.broadcast.emit('BroadCastExceptMe',{data:"HAVE A NICE DAY"});

Conclusion:- Now it will totally depends our business requirement that which one will be preferable.

Anshu Ashish
  • 111
  • 2
  • 6
2

To make it quite simple, consider the following example: there are 2 clients client A, client B and a server we are using in response to certain event emitted by the client.

socket.brodcast.emit()

client A sends the event In this case, the server will not send the event back to client A but it will send the events to all others connected sockets. So in this case only the client B will get the event response

io.emit()

client A sends the event, the server will send the reply event to all the connected sockets both client A and client B