Optional
rooms: Set<string>Optional
exceptRooms: Set<string>Optional
flags: BroadcastFlags & { expectSingleResponse?: boolean }a new BroadcastOperator instance for chaining
Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to receive messages (because of network slowness or other issues, or because they’re connected through long polling and is in the middle of a request-response cycle).
a new BroadcastOperator instance
Gets a list of clients.
this method will be removed in the next major release, please use Server#serverSideEmit or fetchSockets instead.
Sets the compress flag.
if true
, compresses the sending data
a new BroadcastOperator instance
Makes the matching socket instances disconnect.
Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.
Optional
close: booleanwhether to close the underlying connection
Emits to all clients.
Always true
// the “foo” event will be broadcast to all connected clients
io.emit("foo", "bar");
// the “foo” event will be broadcast to all connected clients in the “room-101” room
io.to("room-101").emit("foo", "bar");
// with an acknowledgement expected from all connected clients
io.timeout(1000).emit("some-event", (err, responses) => {
if (err) {
// some clients did not acknowledge the event in the given delay
} else {
console.log(responses); // one response per client
}
});
Emits an event and waits for an acknowledgement from all clients.
a Promise that will be fulfilled when all clients have acknowledged the event
Excludes a room when emitting.
a room, or an array of rooms
a new BroadcastOperator instance for chaining
// the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
io.except("room-101").emit("foo", "bar");
// with an array of rooms
io.except(["room-101", "room-102"]).emit("foo", "bar");
// with multiple chained calls
io.except("room-101").except("room-102").emit("foo", "bar");
Returns the matching socket instances. This method works across a cluster of several Socket.IO servers.
Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.
// return all Socket instances
const sockets = await io.fetchSockets();
// return all Socket instances in the "room1" room
const sockets = await io.in("room1").fetchSockets();
for (const socket of sockets) {
console.log(socket.id);
console.log(socket.handshake);
console.log(socket.rooms);
console.log(socket.data);
socket.emit("hello");
socket.join("room1");
socket.leave("room2");
socket.disconnect();
}
Targets a room when emitting. Similar to to()
, but might feel clearer in some cases:
a room, or an array of rooms
a new BroadcastOperator instance for chaining
Makes the matching socket instances join the specified rooms.
Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.
a room, or an array of rooms
Makes the matching socket instances leave the specified rooms.
Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.
a room, or an array of rooms
Adds a timeout in milliseconds for the next operation
Targets a room when emitting.
a room, or an array of rooms
a new BroadcastOperator instance for chaining
// the “foo” event will be broadcast to all connected clients in the “room-101” room
io.to("room-101").emit("foo", "bar");
// with an array of rooms (a client will be notified at most once)
io.to(["room-101", "room-102"]).emit("foo", "bar");
// with multiple chained calls
io.to("room-101").to("room-102").emit("foo", "bar");
Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.