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:
socket.on('cursor', function(data) {
socket.broadcast.emit('response', data);
});
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:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3