Sending Response to All Clients Except Sender
To broadcast a message to all connected clients, the io.sockets.emit() function is used. However, when you want to exclude the sender from receiving the broadcast, you may wonder about a more straightforward approach than checking the sender's ID on the client-side.
In Socket.IO, the socket.broadcast property provides a solution for this scenario. By using socket.broadcast.emit(), you can send a message to all clients except the sender. Here's an example:
<code class="javascript">socket.on('cursor', function(data) { socket.broadcast.emit('response', data); });</code>
In this example, when a client sends a 'cursor' event, the server broadcasts the received data to all other connected clients, excluding the sender.
Here's a summary of Socket.IO emit functions for your reference:
The above is the detailed content of How to Exclude the Sender from Socket.IO Broadcasts?. For more information, please follow other related articles on the PHP Chinese website!