MCPcopy
hub / github.com/socketio/socket.io / BroadcastOperator

Class BroadcastOperator

packages/socket.io/lib/broadcast-operator.ts:20–476  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18const debug = debugModule("socket.io:broadcast-operator");
19
20export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
21 implements TypedEventBroadcaster<EmitEvents>
22{
23 constructor(
24 private readonly adapter: Adapter,
25 private readonly rooms: Set<Room> = new Set<Room>(),
26 private readonly exceptRooms: Set<Room> = new Set<Room>(),
27 private readonly flags: BroadcastFlags & {
28 expectSingleResponse?: boolean;
29 } = {},
30 ) {}
31
32 /**
33 * Targets a room when emitting.
34 *
35 * @example
36 * // the “foo” event will be broadcast to all connected clients in the “room-101” room
37 * io.to("room-101").emit("foo", "bar");
38 *
39 * // with an array of rooms (a client will be notified at most once)
40 * io.to(["room-101", "room-102"]).emit("foo", "bar");
41 *
42 * // with multiple chained calls
43 * io.to("room-101").to("room-102").emit("foo", "bar");
44 *
45 * @param room - a room, or an array of rooms
46 * @return a new {@link BroadcastOperator} instance for chaining
47 */
48 public to(room: Room | Room[]) {
49 const rooms = new Set(this.rooms);
50 if (Array.isArray(room)) {
51 room.forEach((r) => rooms.add(r));
52 } else {
53 rooms.add(room);
54 }
55 return new BroadcastOperator<EmitEvents, SocketData>(
56 this.adapter,
57 rooms,
58 this.exceptRooms,
59 this.flags,
60 );
61 }
62
63 /**
64 * Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
65 *
66 * @example
67 * // disconnect all clients in the "room-101" room
68 * io.in("room-101").disconnectSockets();
69 *
70 * @param room - a room, or an array of rooms
71 * @return a new {@link BroadcastOperator} instance for chaining
72 */
73 public in(room: Room | Room[]) {
74 return this.to(room);
75 }
76
77 /**

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…