Home >Web Front-end >JS Tutorial >How do I send responses to all clients except the sender in Socket.IO?

How do I send responses to all clients except the sender in Socket.IO?

DDD
DDDOriginal
2024-10-27 09:42:03268browse

How do I send responses to all clients except the sender in Socket.IO?

How to Send Responses to All Clients Except the Sender

In Socket.IO, you can use various methods to send data between the server and clients. For sending to all clients, you typically use io.sockets.emit(). To receive data from clients, use socket.on().

To specifically send messages to all clients except the sender, you can utilize the socket.broadcast.emit() method. This method allows you to broadcast messages to all connected clients except the client that originally sent the data.

Here's an example:

<code class="javascript">socket.on('cursor', function(data) {
  socket.broadcast.emit('response', data);
});</code>

By using this approach, when a message is received from a client, the server will forward that message to all other connected clients, excluding the sender.

The above is the detailed content of How do I send responses to all clients except the sender in Socket.IO?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn