@@ -26,8 +26,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
26
26
/**
27
27
* Targets a room when emitting.
28
28
*
29
- * @param room
30
- * @return a new BroadcastOperator instance
29
+ * @example
30
+ * // the “foo” event will be broadcast to all connected clients in the “room-101” room
31
+ * io.to("room-101").emit("foo", "bar");
32
+ *
33
+ * // with an array of rooms (a client will be notified at most once)
34
+ * io.to(["room-101", "room-102"]).emit("foo", "bar");
35
+ *
36
+ * // with multiple chained calls
37
+ * io.to("room-101").to("room-102").emit("foo", "bar");
38
+ *
39
+ * @param room - a room, or an array of rooms
40
+ * @return a new {@link BroadcastOperator} instance for chaining
31
41
*/
32
42
public to ( room : Room | Room [ ] ) : BroadcastOperator < EmitEvents , SocketData > {
33
43
const rooms = new Set ( this . rooms ) ;
@@ -45,10 +55,14 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
45
55
}
46
56
47
57
/**
48
- * Targets a room when emitting.
58
+ * Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
49
59
*
50
- * @param room
51
- * @return a new BroadcastOperator instance
60
+ * @example
61
+ * // disconnect all clients in the "room-101" room
62
+ * io.in("room-101").disconnectSockets();
63
+ *
64
+ * @param room - a room, or an array of rooms
65
+ * @return a new {@link BroadcastOperator} instance for chaining
52
66
*/
53
67
public in ( room : Room | Room [ ] ) : BroadcastOperator < EmitEvents , SocketData > {
54
68
return this . to ( room ) ;
@@ -57,8 +71,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
57
71
/**
58
72
* Excludes a room when emitting.
59
73
*
60
- * @param room
61
- * @return a new BroadcastOperator instance
74
+ * @example
75
+ * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
76
+ * io.except("room-101").emit("foo", "bar");
77
+ *
78
+ * // with an array of rooms
79
+ * io.except(["room-101", "room-102"]).emit("foo", "bar");
80
+ *
81
+ * // with multiple chained calls
82
+ * io.except("room-101").except("room-102").emit("foo", "bar");
83
+ *
84
+ * @param room - a room, or an array of rooms
85
+ * @return a new {@link BroadcastOperator} instance for chaining
62
86
*/
63
87
public except (
64
88
room : Room | Room [ ] ,
@@ -82,7 +106,10 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
82
106
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
83
107
* and is in the middle of a request-response cycle).
84
108
*
85
- * @return a new BroadcastOperator instance
109
+ * @example
110
+ * io.volatile.emit("hello"); // the clients may or may not receive it
111
+ *
112
+ * @return a new {@link BroadcastOperator} instance for chaining
86
113
*/
87
114
public get volatile ( ) : BroadcastOperator < EmitEvents , SocketData > {
88
115
const flags = Object . assign ( { } , this . flags , { volatile : true } ) ;
@@ -97,7 +124,11 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
97
124
/**
98
125
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
99
126
*
100
- * @return a new BroadcastOperator instance
127
+ * @example
128
+ * // the “foo” event will be broadcast to all connected clients on this node
129
+ * io.local.emit("foo", "bar");
130
+ *
131
+ * @return a new {@link BroadcastOperator} instance for chaining
101
132
*/
102
133
public get local ( ) : BroadcastOperator < EmitEvents , SocketData > {
103
134
const flags = Object . assign ( { } , this . flags , { local : true } ) ;
@@ -112,14 +143,15 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
112
143
/**
113
144
* Adds a timeout in milliseconds for the next operation
114
145
*
115
- * <pre><code>
116
- *
146
+ * @example
117
147
* io.timeout(1000).emit("some-event", (err, responses) => {
118
- * // ...
148
+ * if (err) {
149
+ * // some clients did not acknowledge the event in the given delay
150
+ * } else {
151
+ * console.log(responses); // one response per client
152
+ * }
119
153
* });
120
154
*
121
- * </pre></code>
122
- *
123
155
* @param timeout
124
156
*/
125
157
public timeout ( timeout : number ) {
@@ -135,6 +167,22 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
135
167
/**
136
168
* Emits to all clients.
137
169
*
170
+ * @example
171
+ * // the “foo” event will be broadcast to all connected clients
172
+ * io.emit("foo", "bar");
173
+ *
174
+ * // the “foo” event will be broadcast to all connected clients in the “room-101” room
175
+ * io.to("room-101").emit("foo", "bar");
176
+ *
177
+ * // with an acknowledgement expected from all connected clients
178
+ * io.timeout(1000).emit("some-event", (err, responses) => {
179
+ * if (err) {
180
+ * // some clients did not acknowledge the event in the given delay
181
+ * } else {
182
+ * console.log(responses); // one response per client
183
+ * }
184
+ * });
185
+ *
138
186
* @return Always true
139
187
*/
140
188
public emit < Ev extends EventNames < EmitEvents > > (
@@ -218,7 +266,26 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
218
266
}
219
267
220
268
/**
221
- * Returns the matching socket instances
269
+ * Returns the matching socket instances. This method works across a cluster of several Socket.IO servers.
270
+ *
271
+ * @example
272
+ * // return all Socket instances
273
+ * const sockets = await io.fetchSockets();
274
+ *
275
+ * // return all Socket instances in the "room1" room
276
+ * const sockets = await io.in("room1").fetchSockets();
277
+ *
278
+ * for (const socket of sockets) {
279
+ * console.log(socket.id);
280
+ * console.log(socket.handshake);
281
+ * console.log(socket.rooms);
282
+ * console.log(socket.data);
283
+ *
284
+ * socket.emit("hello");
285
+ * socket.join("room1");
286
+ * socket.leave("room2");
287
+ * socket.disconnect();
288
+ * }
222
289
*/
223
290
public fetchSockets < SocketData = unknown > ( ) : Promise <
224
291
RemoteSocket < EmitEvents , SocketData > [ ]
@@ -245,9 +312,19 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
245
312
}
246
313
247
314
/**
248
- * Makes the matching socket instances join the specified rooms
315
+ * Makes the matching socket instances join the specified rooms.
316
+ *
317
+ * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
318
+ *
319
+ * @example
320
+ *
321
+ * // make all socket instances join the "room1" room
322
+ * io.socketsJoin("room1");
249
323
*
250
- * @param room
324
+ * // make all socket instances in the "room1" room join the "room2" and "room3" rooms
325
+ * io.in("room1").socketsJoin(["room2", "room3"]);
326
+ *
327
+ * @param room - a room, or an array of rooms
251
328
*/
252
329
public socketsJoin ( room : Room | Room [ ] ) : void {
253
330
this . adapter . addSockets (
@@ -261,9 +338,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
261
338
}
262
339
263
340
/**
264
- * Makes the matching socket instances leave the specified rooms
341
+ * Makes the matching socket instances leave the specified rooms.
342
+ *
343
+ * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
344
+ *
345
+ * @example
346
+ * // make all socket instances leave the "room1" room
347
+ * io.socketsLeave("room1");
348
+ *
349
+ * // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
350
+ * io.in("room1").socketsLeave(["room2", "room3"]);
265
351
*
266
- * @param room
352
+ * @param room - a room, or an array of rooms
267
353
*/
268
354
public socketsLeave ( room : Room | Room [ ] ) : void {
269
355
this . adapter . delSockets (
@@ -277,7 +363,16 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
277
363
}
278
364
279
365
/**
280
- * Makes the matching socket instances disconnect
366
+ * Makes the matching socket instances disconnect.
367
+ *
368
+ * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
369
+ *
370
+ * @example
371
+ * // make all socket instances disconnect (the connections might be kept alive for other namespaces)
372
+ * io.disconnectSockets();
373
+ *
374
+ * // make all socket instances in the "room1" room disconnect and close the underlying connections
375
+ * io.in("room1").disconnectSockets(true);
281
376
*
282
377
* @param close - whether to close the underlying connection
283
378
*/
0 commit comments