Skip to content

Commit ec07bed

Browse files
docs: add some jsdoc for each public method
1 parent 518f534 commit ec07bed

File tree

6 files changed

+794
-123
lines changed

6 files changed

+794
-123
lines changed

mod.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
/**
2+
* @example
3+
* import { serve } from "https://deno.land/[email protected]/http/server.ts";
4+
* import { Server } from "https://deno.land/x/[email protected]/mod.ts";
5+
*
6+
* const io = new Server();
7+
*
8+
* io.on("connection", (socket) => {
9+
* console.log(`socket ${socket.id} connected`);
10+
*
11+
* // send an event to the client
12+
* socket.emit("foo", "bar");
13+
*
14+
* socket.on("foobar", () => {
15+
* // an event was received from the client
16+
* });
17+
*
18+
* // upon disconnection
19+
* socket.on("disconnect", (reason) => {
20+
* console.log(`socket ${socket.id} disconnected due to ${reason}`);
21+
* });
22+
* });
23+
*
24+
* await serve(io.handler(), {
25+
* port: 3000,
26+
* });
27+
*/
128
export {
229
Adapter,
330
type BroadcastOptions,
@@ -12,9 +39,7 @@ export {
1239
*
1340
* Documentation: https://socket.io/docs/v4/redis-adapter/
1441
*
15-
* Usage:
16-
*
17-
* ```
42+
* @example
1843
* import { serve } from "https://deno.land/std/http/server.ts";
1944
* import { Server, createRedisAdapter, createRedisClient } from "https://deno.land/x/socket_io/mod.ts";
2045
*
@@ -34,7 +59,6 @@ export {
3459
* await serve(io.handler(), {
3560
* port: 3000
3661
* });
37-
* ```
3862
*/
3963
export {
4064
createAdapter as createRedisAdapter,

packages/socket.io/lib/broadcast-operator.ts

Lines changed: 115 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
2626
/**
2727
* Targets a room when emitting.
2828
*
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
3141
*/
3242
public to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
3343
const rooms = new Set(this.rooms);
@@ -45,10 +55,14 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
4555
}
4656

4757
/**
48-
* Targets a room when emitting.
58+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
4959
*
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
5266
*/
5367
public in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
5468
return this.to(room);
@@ -57,8 +71,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
5771
/**
5872
* Excludes a room when emitting.
5973
*
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
6286
*/
6387
public except(
6488
room: Room | Room[],
@@ -82,7 +106,10 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
82106
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
83107
* and is in the middle of a request-response cycle).
84108
*
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
86113
*/
87114
public get volatile(): BroadcastOperator<EmitEvents, SocketData> {
88115
const flags = Object.assign({}, this.flags, { volatile: true });
@@ -97,7 +124,11 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
97124
/**
98125
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
99126
*
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
101132
*/
102133
public get local(): BroadcastOperator<EmitEvents, SocketData> {
103134
const flags = Object.assign({}, this.flags, { local: true });
@@ -112,14 +143,15 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
112143
/**
113144
* Adds a timeout in milliseconds for the next operation
114145
*
115-
* <pre><code>
116-
*
146+
* @example
117147
* 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+
* }
119153
* });
120154
*
121-
* </pre></code>
122-
*
123155
* @param timeout
124156
*/
125157
public timeout(timeout: number) {
@@ -135,6 +167,22 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
135167
/**
136168
* Emits to all clients.
137169
*
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+
*
138186
* @return Always true
139187
*/
140188
public emit<Ev extends EventNames<EmitEvents>>(
@@ -218,7 +266,26 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
218266
}
219267

220268
/**
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+
* }
222289
*/
223290
public fetchSockets<SocketData = unknown>(): Promise<
224291
RemoteSocket<EmitEvents, SocketData>[]
@@ -245,9 +312,19 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
245312
}
246313

247314
/**
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");
249323
*
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
251328
*/
252329
public socketsJoin(room: Room | Room[]): void {
253330
this.adapter.addSockets(
@@ -261,9 +338,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
261338
}
262339

263340
/**
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"]);
265351
*
266-
* @param room
352+
* @param room - a room, or an array of rooms
267353
*/
268354
public socketsLeave(room: Room | Room[]): void {
269355
this.adapter.delSockets(
@@ -277,7 +363,16 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
277363
}
278364

279365
/**
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);
281376
*
282377
* @param close - whether to close the underlying connection
283378
*/

packages/socket.io/lib/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ interface WriteOptions {
1111
volatile?: boolean;
1212
}
1313

14+
/**
15+
* A {@link Client} can be associated with many multiplexed {@link Socket} that belong to different {@link Namespace}.
16+
*/
1417
export class Client<
1518
ListenEvents extends EventsMap,
1619
EmitEvents extends EventsMap,

0 commit comments

Comments
 (0)