What's the difference between io.sockets.emit and broadcast?

后端 未结 3 1585
逝去的感伤
逝去的感伤 2020-11-29 18:25

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 the

相关标签:
3条回答
  • 2020-11-29 18:36

    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.

    0 讨论(0)
  • 2020-11-29 18:40

    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.

    0 讨论(0)
  • 2020-11-29 19:00

    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:

    0 讨论(0)
提交回复
热议问题