Skip to content

Commit 8fae95d

Browse files
committedOct 14, 2022
docs: add jsdoc for each public method
1 parent e6f6b90 commit 8fae95d

File tree

4 files changed

+822
-231
lines changed

4 files changed

+822
-231
lines changed
 

‎lib/broadcast-operator.ts

+120-35
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
2222
/**
2323
* Targets a room when emitting.
2424
*
25-
* @param room
26-
* @return a new BroadcastOperator instance
27-
* @public
25+
* @example
26+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
27+
* io.to("room-101").emit("foo", "bar");
28+
*
29+
* // with an array of rooms (a client will be notified at most once)
30+
* io.to(["room-101", "room-102"]).emit("foo", "bar");
31+
*
32+
* // with multiple chained calls
33+
* io.to("room-101").to("room-102").emit("foo", "bar");
34+
*
35+
* @param room - a room, or an array of rooms
36+
* @return a new {@link BroadcastOperator} instance for chaining
2837
*/
2938
public to(room: Room | Room[]) {
3039
const rooms = new Set(this.rooms);
@@ -42,11 +51,14 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
4251
}
4352

4453
/**
45-
* Targets a room when emitting.
54+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
4655
*
47-
* @param room
48-
* @return a new BroadcastOperator instance
49-
* @public
56+
* @example
57+
* // disconnect all clients in the "room-101" room
58+
* io.in("room-101").disconnectSockets();
59+
*
60+
* @param room - a room, or an array of rooms
61+
* @return a new {@link BroadcastOperator} instance for chaining
5062
*/
5163
public in(room: Room | Room[]) {
5264
return this.to(room);
@@ -55,9 +67,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
5567
/**
5668
* Excludes a room when emitting.
5769
*
58-
* @param room
59-
* @return a new BroadcastOperator instance
60-
* @public
70+
* @example
71+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
72+
* io.except("room-101").emit("foo", "bar");
73+
*
74+
* // with an array of rooms
75+
* io.except(["room-101", "room-102"]).emit("foo", "bar");
76+
*
77+
* // with multiple chained calls
78+
* io.except("room-101").except("room-102").emit("foo", "bar");
79+
*
80+
* @param room - a room, or an array of rooms
81+
* @return a new {@link BroadcastOperator} instance for chaining
6182
*/
6283
public except(room: Room | Room[]) {
6384
const exceptRooms = new Set(this.exceptRooms);
@@ -77,9 +98,11 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
7798
/**
7899
* Sets the compress flag.
79100
*
101+
* @example
102+
* io.compress(false).emit("hello");
103+
*
80104
* @param compress - if `true`, compresses the sending data
81105
* @return a new BroadcastOperator instance
82-
* @public
83106
*/
84107
public compress(compress: boolean) {
85108
const flags = Object.assign({}, this.flags, { compress });
@@ -96,8 +119,10 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
96119
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
97120
* and is in the middle of a request-response cycle).
98121
*
122+
* @example
123+
* io.volatile.emit("hello"); // the clients may or may not receive it
124+
*
99125
* @return a new BroadcastOperator instance
100-
* @public
101126
*/
102127
public get volatile() {
103128
const flags = Object.assign({}, this.flags, { volatile: true });
@@ -112,8 +137,11 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
112137
/**
113138
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
114139
*
115-
* @return a new BroadcastOperator instance
116-
* @public
140+
* @example
141+
* // the “foo” event will be broadcast to all connected clients on this node
142+
* io.local.emit("foo", "bar");
143+
*
144+
* @return a new {@link BroadcastOperator} instance for chaining
117145
*/
118146
public get local() {
119147
const flags = Object.assign({}, this.flags, { local: true });
@@ -128,14 +156,15 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
128156
/**
129157
* Adds a timeout in milliseconds for the next operation
130158
*
131-
* <pre><code>
132-
*
159+
* @example
133160
* io.timeout(1000).emit("some-event", (err, responses) => {
134-
* // ...
161+
* if (err) {
162+
* // some clients did not acknowledge the event in the given delay
163+
* } else {
164+
* console.log(responses); // one response per client
165+
* }
135166
* });
136167
*
137-
* </pre></code>
138-
*
139168
* @param timeout
140169
*/
141170
public timeout(timeout: number) {
@@ -151,8 +180,23 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
151180
/**
152181
* Emits to all clients.
153182
*
183+
* @example
184+
* // the “foo” event will be broadcast to all connected clients
185+
* io.emit("foo", "bar");
186+
*
187+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
188+
* io.to("room-101").emit("foo", "bar");
189+
*
190+
* // with an acknowledgement expected from all connected clients
191+
* io.timeout(1000).emit("some-event", (err, responses) => {
192+
* if (err) {
193+
* // some clients did not acknowledge the event in the given delay
194+
* } else {
195+
* console.log(responses); // one response per client
196+
* }
197+
* });
198+
*
154199
* @return Always true
155-
* @public
156200
*/
157201
public emit<Ev extends EventNames<EmitEvents>>(
158202
ev: Ev,
@@ -235,7 +279,8 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
235279
/**
236280
* Gets a list of clients.
237281
*
238-
* @public
282+
* @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
283+
* {@link fetchSockets} instead.
239284
*/
240285
public allSockets(): Promise<Set<SocketId>> {
241286
if (!this.adapter) {
@@ -247,9 +292,28 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
247292
}
248293

249294
/**
250-
* Returns the matching socket instances
295+
* Returns the matching socket instances. This method works across a cluster of several Socket.IO servers.
296+
*
297+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
298+
*
299+
* @example
300+
* // return all Socket instances
301+
* const sockets = await io.fetchSockets();
251302
*
252-
* @public
303+
* // return all Socket instances in the "room1" room
304+
* const sockets = await io.in("room1").fetchSockets();
305+
*
306+
* for (const socket of sockets) {
307+
* console.log(socket.id);
308+
* console.log(socket.handshake);
309+
* console.log(socket.rooms);
310+
* console.log(socket.data);
311+
*
312+
* socket.emit("hello");
313+
* socket.join("room1");
314+
* socket.leave("room2");
315+
* socket.disconnect();
316+
* }
253317
*/
254318
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
255319
return this.adapter
@@ -274,10 +338,19 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
274338
}
275339

276340
/**
277-
* Makes the matching socket instances join the specified rooms
341+
* Makes the matching socket instances join 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
278346
*
279-
* @param room
280-
* @public
347+
* // make all socket instances join the "room1" room
348+
* io.socketsJoin("room1");
349+
*
350+
* // make all socket instances in the "room1" room join the "room2" and "room3" rooms
351+
* io.in("room1").socketsJoin(["room2", "room3"]);
352+
*
353+
* @param room - a room, or an array of rooms
281354
*/
282355
public socketsJoin(room: Room | Room[]): void {
283356
this.adapter.addSockets(
@@ -291,10 +364,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
291364
}
292365

293366
/**
294-
* Makes the matching socket instances leave the specified rooms
367+
* Makes the matching socket instances leave the specified rooms.
368+
*
369+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
370+
*
371+
* @example
372+
* // make all socket instances leave the "room1" room
373+
* io.socketsLeave("room1");
374+
*
375+
* // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
376+
* io.in("room1").socketsLeave(["room2", "room3"]);
295377
*
296-
* @param room
297-
* @public
378+
* @param room - a room, or an array of rooms
298379
*/
299380
public socketsLeave(room: Room | Room[]): void {
300381
this.adapter.delSockets(
@@ -308,10 +389,18 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
308389
}
309390

310391
/**
311-
* Makes the matching socket instances disconnect
392+
* Makes the matching socket instances disconnect.
393+
*
394+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
395+
*
396+
* @example
397+
* // make all socket instances disconnect (the connections might be kept alive for other namespaces)
398+
* io.disconnectSockets();
399+
*
400+
* // make all socket instances in the "room1" room disconnect and close the underlying connections
401+
* io.in("room1").disconnectSockets(true);
312402
*
313403
* @param close - whether to close the underlying connection
314-
* @public
315404
*/
316405
public disconnectSockets(close: boolean = false): void {
317406
this.adapter.disconnectSockets(
@@ -370,7 +459,6 @@ export class RemoteSocket<EmitEvents extends EventsMap, SocketData>
370459
* Joins a room.
371460
*
372461
* @param {String|Array} room - room or array of rooms
373-
* @public
374462
*/
375463
public join(room: Room | Room[]): void {
376464
return this.operator.socketsJoin(room);
@@ -380,7 +468,6 @@ export class RemoteSocket<EmitEvents extends EventsMap, SocketData>
380468
* Leaves a room.
381469
*
382470
* @param {String} room
383-
* @public
384471
*/
385472
public leave(room: Room): void {
386473
return this.operator.socketsLeave(room);
@@ -391,8 +478,6 @@ export class RemoteSocket<EmitEvents extends EventsMap, SocketData>
391478
*
392479
* @param {Boolean} close - if `true`, closes the underlying connection
393480
* @return {Socket} self
394-
*
395-
* @public
396481
*/
397482
public disconnect(close = false): this {
398483
this.operator.disconnectSockets(close);

‎lib/index.ts

+193-69
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ interface ServerOptions extends EngineOptions, AttachOptions {
7272
connectTimeout: number;
7373
}
7474

75+
/**
76+
* Represents a Socket.IO server.
77+
*
78+
* @example
79+
* import { Server } from "socket.io";
80+
*
81+
* const io = new Server();
82+
*
83+
* io.on("connection", (socket) => {
84+
* console.log(`socket ${socket.id} connected`);
85+
*
86+
* // send an event to the client
87+
* socket.emit("foo", "bar");
88+
*
89+
* socket.on("foobar", () => {
90+
* // an event was received from the client
91+
* });
92+
*
93+
* // upon disconnection
94+
* socket.on("disconnect", (reason) => {
95+
* console.log(`socket ${socket.id} disconnected due to ${reason}`);
96+
* });
97+
* });
98+
*
99+
* io.listen(3000);
100+
*/
75101
export class Server<
76102
ListenEvents extends EventsMap = DefaultEventsMap,
77103
EmitEvents extends EventsMap = ListenEvents,
@@ -96,11 +122,8 @@ export class Server<
96122
/**
97123
* A reference to the underlying Engine.IO server.
98124
*
99-
* Example:
100-
*
101-
* <code>
102-
* const clientsCount = io.engine.clientsCount;
103-
* </code>
125+
* @example
126+
* const clientsCount = io.engine.clientsCount;
104127
*
105128
*/
106129
public engine: any;
@@ -139,7 +162,6 @@ export class Server<
139162
*
140163
* @param srv http server, port, or options
141164
* @param [opts]
142-
* @public
143165
*/
144166
constructor(opts?: Partial<ServerOptions>);
145167
constructor(
@@ -190,7 +212,6 @@ export class Server<
190212
*
191213
* @param v - whether to serve client code
192214
* @return self when setting or value when getting
193-
* @public
194215
*/
195216
public serveClient(v: boolean): this;
196217
public serveClient(): boolean;
@@ -253,7 +274,6 @@ export class Server<
253274
*
254275
* @param {String} v pathname
255276
* @return {Server|String} self when setting or value when getting
256-
* @public
257277
*/
258278
public path(v: string): this;
259279
public path(): string;
@@ -275,7 +295,6 @@ export class Server<
275295
/**
276296
* Set the delay after which a client without namespace is closed
277297
* @param v
278-
* @public
279298
*/
280299
public connectTimeout(v: number): this;
281300
public connectTimeout(): number;
@@ -291,7 +310,6 @@ export class Server<
291310
*
292311
* @param v pathname
293312
* @return self when setting or value when getting
294-
* @public
295313
*/
296314
public adapter(): AdapterConstructor | undefined;
297315
public adapter(v: AdapterConstructor): this;
@@ -312,7 +330,6 @@ export class Server<
312330
* @param srv - server or port
313331
* @param opts - options passed to engine.io
314332
* @return self
315-
* @public
316333
*/
317334
public listen(
318335
srv: http.Server | HTTPSServer | number,
@@ -327,7 +344,6 @@ export class Server<
327344
* @param srv - server or port
328345
* @param opts - options passed to engine.io
329346
* @return self
330-
* @public
331347
*/
332348
public attach(
333349
srv: http.Server | HTTPSServer | number,
@@ -561,7 +577,6 @@ export class Server<
561577
*
562578
* @param {engine.Server} engine engine.io (or compatible) server
563579
* @return self
564-
* @public
565580
*/
566581
public bind(engine): this {
567582
this.engine = engine;
@@ -589,9 +604,20 @@ export class Server<
589604
/**
590605
* Looks up a namespace.
591606
*
592-
* @param {String|RegExp|Function} name nsp name
607+
* @example
608+
* // with a simple string
609+
* const myNamespace = io.of("/my-namespace");
610+
*
611+
* // with a regex
612+
* const dynamicNsp = io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
613+
* const namespace = socket.nsp; // newNamespace.name === "/dynamic-101"
614+
*
615+
* // broadcast to all clients in the given sub-namespace
616+
* namespace.emit("hello");
617+
* });
618+
*
619+
* @param name - nsp name
593620
* @param fn optional, nsp `connection` ev handler
594-
* @public
595621
*/
596622
public of(
597623
name: string | RegExp | ParentNspNameMatchFn,
@@ -637,7 +663,6 @@ export class Server<
637663
* Closes server connection
638664
*
639665
* @param [fn] optional, called as `fn([err])` on error OR all conns closed
640-
* @public
641666
*/
642667
public close(fn?: (err?: Error) => void): void {
643668
for (const socket of this.sockets.sockets.values()) {
@@ -657,10 +682,15 @@ export class Server<
657682
}
658683

659684
/**
660-
* Sets up namespace middleware.
685+
* Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
661686
*
662-
* @return self
663-
* @public
687+
* @example
688+
* io.use((socket, next) => {
689+
* // ...
690+
* next();
691+
* });
692+
*
693+
* @param fn - the middleware function
664694
*/
665695
public use(
666696
fn: (
@@ -675,66 +705,112 @@ export class Server<
675705
/**
676706
* Targets a room when emitting.
677707
*
678-
* @param room
679-
* @return self
680-
* @public
708+
* @example
709+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
710+
* io.to("room-101").emit("foo", "bar");
711+
*
712+
* // with an array of rooms (a client will be notified at most once)
713+
* io.to(["room-101", "room-102"]).emit("foo", "bar");
714+
*
715+
* // with multiple chained calls
716+
* io.to("room-101").to("room-102").emit("foo", "bar");
717+
*
718+
* @param room - a room, or an array of rooms
719+
* @return a new {@link BroadcastOperator} instance for chaining
681720
*/
682-
public to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
721+
public to(room: Room | Room[]) {
683722
return this.sockets.to(room);
684723
}
685724

686725
/**
687-
* Targets a room when emitting.
726+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
688727
*
689-
* @param room
690-
* @return self
691-
* @public
728+
* @example
729+
* // disconnect all clients in the "room-101" room
730+
* io.in("room-101").disconnectSockets();
731+
*
732+
* @param room - a room, or an array of rooms
733+
* @return a new {@link BroadcastOperator} instance for chaining
692734
*/
693-
public in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
735+
public in(room: Room | Room[]) {
694736
return this.sockets.in(room);
695737
}
696738

697739
/**
698740
* Excludes a room when emitting.
699741
*
700-
* @param name
701-
* @return self
702-
* @public
742+
* @example
743+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
744+
* io.except("room-101").emit("foo", "bar");
745+
*
746+
* // with an array of rooms
747+
* io.except(["room-101", "room-102"]).emit("foo", "bar");
748+
*
749+
* // with multiple chained calls
750+
* io.except("room-101").except("room-102").emit("foo", "bar");
751+
*
752+
* @param room - a room, or an array of rooms
753+
* @return a new {@link BroadcastOperator} instance for chaining
703754
*/
704-
public except(
705-
name: Room | Room[]
706-
): BroadcastOperator<EmitEvents, SocketData> {
707-
return this.sockets.except(name);
755+
public except(room: Room | Room[]) {
756+
return this.sockets.except(room);
708757
}
709758

710759
/**
711760
* Sends a `message` event to all clients.
712761
*
762+
* This method mimics the WebSocket.send() method.
763+
*
764+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
765+
*
766+
* @example
767+
* io.send("hello");
768+
*
769+
* // this is equivalent to
770+
* io.emit("message", "hello");
771+
*
713772
* @return self
714-
* @public
715773
*/
716774
public send(...args: EventParams<EmitEvents, "message">): this {
717775
this.sockets.emit("message", ...args);
718776
return this;
719777
}
720778

721779
/**
722-
* Sends a `message` event to all clients.
780+
* Sends a `message` event to all clients. Alias of {@link send}.
723781
*
724782
* @return self
725-
* @public
726783
*/
727784
public write(...args: EventParams<EmitEvents, "message">): this {
728785
this.sockets.emit("message", ...args);
729786
return this;
730787
}
731788

732789
/**
733-
* Emit a packet to other Socket.IO servers
790+
* Sends a message to the other Socket.IO servers of the cluster.
791+
*
792+
* @example
793+
* io.serverSideEmit("hello", "world");
794+
*
795+
* io.on("hello", (arg1) => {
796+
* console.log(arg1); // prints "world"
797+
* });
798+
*
799+
* // acknowledgements (without binary content) are supported too:
800+
* io.serverSideEmit("ping", (err, responses) => {
801+
* if (err) {
802+
* // some clients did not acknowledge the event in the given delay
803+
* } else {
804+
* console.log(responses); // one response per client
805+
* }
806+
* });
807+
*
808+
* io.on("ping", (cb) => {
809+
* cb("pong");
810+
* });
734811
*
735812
* @param ev - the event name
736813
* @param args - an array of arguments, which may include an acknowledgement callback at the end
737-
* @public
738814
*/
739815
public serverSideEmit<Ev extends EventNames<ServerSideEvents>>(
740816
ev: Ev,
@@ -748,8 +824,6 @@ export class Server<
748824
*
749825
* @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
750826
* {@link Server#fetchSockets} instead.
751-
*
752-
* @public
753827
*/
754828
public allSockets(): Promise<Set<SocketId>> {
755829
return this.sockets.allSockets();
@@ -758,13 +832,13 @@ export class Server<
758832
/**
759833
* Sets the compress flag.
760834
*
835+
* @example
836+
* io.compress(false).emit("hello");
837+
*
761838
* @param compress - if `true`, compresses the sending data
762-
* @return self
763-
* @public
839+
* @return a new {@link BroadcastOperator} instance for chaining
764840
*/
765-
public compress(
766-
compress: boolean
767-
): BroadcastOperator<EmitEvents, SocketData> {
841+
public compress(compress: boolean) {
768842
return this.sockets.compress(compress);
769843
}
770844

@@ -773,74 +847,124 @@ export class Server<
773847
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
774848
* and is in the middle of a request-response cycle).
775849
*
776-
* @return self
777-
* @public
850+
* @example
851+
* io.volatile.emit("hello"); // the clients may or may not receive it
852+
*
853+
* @return a new {@link BroadcastOperator} instance for chaining
778854
*/
779-
public get volatile(): BroadcastOperator<EmitEvents, SocketData> {
855+
public get volatile() {
780856
return this.sockets.volatile;
781857
}
782858

783859
/**
784860
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
785861
*
786-
* @return self
787-
* @public
862+
* @example
863+
* // the “foo” event will be broadcast to all connected clients on this node
864+
* io.local.emit("foo", "bar");
865+
*
866+
* @return a new {@link BroadcastOperator} instance for chaining
788867
*/
789-
public get local(): BroadcastOperator<EmitEvents, SocketData> {
868+
public get local() {
790869
return this.sockets.local;
791870
}
792871

793872
/**
794-
* Adds a timeout in milliseconds for the next operation
795-
*
796-
* <pre><code>
873+
* Adds a timeout in milliseconds for the next operation.
797874
*
875+
* @example
798876
* io.timeout(1000).emit("some-event", (err, responses) => {
799-
* // ...
877+
* if (err) {
878+
* // some clients did not acknowledge the event in the given delay
879+
* } else {
880+
* console.log(responses); // one response per client
881+
* }
800882
* });
801883
*
802-
* </pre></code>
803-
*
804884
* @param timeout
805885
*/
806886
public timeout(timeout: number) {
807887
return this.sockets.timeout(timeout);
808888
}
809889

810890
/**
811-
* Returns the matching socket instances
891+
* Returns the matching socket instances.
812892
*
813-
* @public
893+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
894+
*
895+
* @example
896+
* // return all Socket instances
897+
* const sockets = await io.fetchSockets();
898+
*
899+
* // return all Socket instances in the "room1" room
900+
* const sockets = await io.in("room1").fetchSockets();
901+
*
902+
* for (const socket of sockets) {
903+
* console.log(socket.id);
904+
* console.log(socket.handshake);
905+
* console.log(socket.rooms);
906+
* console.log(socket.data);
907+
*
908+
* socket.emit("hello");
909+
* socket.join("room1");
910+
* socket.leave("room2");
911+
* socket.disconnect();
912+
* }
814913
*/
815914
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
816915
return this.sockets.fetchSockets();
817916
}
818917

819918
/**
820-
* Makes the matching socket instances join the specified rooms
919+
* Makes the matching socket instances join the specified rooms.
920+
*
921+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
922+
*
923+
* @example
924+
*
925+
* // make all socket instances join the "room1" room
926+
* io.socketsJoin("room1");
821927
*
822-
* @param room
823-
* @public
928+
* // make all socket instances in the "room1" room join the "room2" and "room3" rooms
929+
* io.in("room1").socketsJoin(["room2", "room3"]);
930+
*
931+
* @param room - a room, or an array of rooms
824932
*/
825933
public socketsJoin(room: Room | Room[]) {
826934
return this.sockets.socketsJoin(room);
827935
}
828936

829937
/**
830-
* Makes the matching socket instances leave the specified rooms
938+
* Makes the matching socket instances leave the specified rooms.
939+
*
940+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
941+
*
942+
* @example
943+
* // make all socket instances leave the "room1" room
944+
* io.socketsLeave("room1");
945+
*
946+
* // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
947+
* io.in("room1").socketsLeave(["room2", "room3"]);
831948
*
832-
* @param room
833-
* @public
949+
* @param room - a room, or an array of rooms
834950
*/
835951
public socketsLeave(room: Room | Room[]) {
836952
return this.sockets.socketsLeave(room);
837953
}
838954

839955
/**
840-
* Makes the matching socket instances disconnect
956+
* Makes the matching socket instances disconnect.
957+
*
958+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
959+
*
960+
* @example
961+
* // make all socket instances disconnect (the connections might be kept alive for other namespaces)
962+
* io.disconnectSockets();
963+
*
964+
* // make all socket instances in the "room1" room disconnect and close the underlying connections
965+
* io.in("room1").disconnectSockets(true);
841966
*
842967
* @param close - whether to close the underlying connection
843-
* @public
844968
*/
845969
public disconnectSockets(close: boolean = false) {
846970
return this.sockets.disconnectSockets(close);

‎lib/namespace.ts

+241-42
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,59 @@ export const RESERVED_EVENTS: ReadonlySet<string | Symbol> = new Set<
5252
keyof ServerReservedEventsMap<never, never, never, never>
5353
>(<const>["connect", "connection", "new_namespace"]);
5454

55+
/**
56+
* A Namespace is a communication channel that allows you to split the logic of your application over a single shared
57+
* connection.
58+
*
59+
* Each namespace has its own:
60+
*
61+
* - event handlers
62+
*
63+
* ```
64+
* io.of("/orders").on("connection", (socket) => {
65+
* socket.on("order:list", () => {});
66+
* socket.on("order:create", () => {});
67+
* });
68+
*
69+
* io.of("/users").on("connection", (socket) => {
70+
* socket.on("user:list", () => {});
71+
* });
72+
* ```
73+
*
74+
* - rooms
75+
*
76+
* ```
77+
* const orderNamespace = io.of("/orders");
78+
*
79+
* orderNamespace.on("connection", (socket) => {
80+
* socket.join("room1");
81+
* orderNamespace.to("room1").emit("hello");
82+
* });
83+
*
84+
* const userNamespace = io.of("/users");
85+
*
86+
* userNamespace.on("connection", (socket) => {
87+
* socket.join("room1"); // distinct from the room in the "orders" namespace
88+
* userNamespace.to("room1").emit("holà");
89+
* });
90+
* ```
91+
*
92+
* - middlewares
93+
*
94+
* ```
95+
* const orderNamespace = io.of("/orders");
96+
*
97+
* orderNamespace.use((socket, next) => {
98+
* // ensure the socket has access to the "orders" namespace
99+
* });
100+
*
101+
* const userNamespace = io.of("/users");
102+
*
103+
* userNamespace.use((socket, next) => {
104+
* // ensure the socket has access to the "users" namespace
105+
* });
106+
* ```
107+
*/
55108
export class Namespace<
56109
ListenEvents extends EventsMap = DefaultEventsMap,
57110
EmitEvents extends EventsMap = ListenEvents,
@@ -123,10 +176,17 @@ export class Namespace<
123176
}
124177

125178
/**
126-
* Sets up namespace middleware.
179+
* Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
127180
*
128-
* @return self
129-
* @public
181+
* @example
182+
* const myNamespace = io.of("/my-namespace");
183+
*
184+
* myNamespace.use((socket, next) => {
185+
* // ...
186+
* next();
187+
* });
188+
*
189+
* @param fn - the middleware function
130190
*/
131191
public use(
132192
fn: (
@@ -171,20 +231,36 @@ export class Namespace<
171231
/**
172232
* Targets a room when emitting.
173233
*
174-
* @param room
175-
* @return self
176-
* @public
234+
* @example
235+
* const myNamespace = io.of("/my-namespace");
236+
*
237+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
238+
* myNamespace.to("room-101").emit("foo", "bar");
239+
*
240+
* // with an array of rooms (a client will be notified at most once)
241+
* myNamespace.to(["room-101", "room-102"]).emit("foo", "bar");
242+
*
243+
* // with multiple chained calls
244+
* myNamespace.to("room-101").to("room-102").emit("foo", "bar");
245+
*
246+
* @param room - a room, or an array of rooms
247+
* @return a new {@link BroadcastOperator} instance for chaining
177248
*/
178249
public to(room: Room | Room[]) {
179250
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).to(room);
180251
}
181252

182253
/**
183-
* Targets a room when emitting.
254+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
184255
*
185-
* @param room
186-
* @return self
187-
* @public
256+
* @example
257+
* const myNamespace = io.of("/my-namespace");
258+
*
259+
* // disconnect all clients in the "room-101" room
260+
* myNamespace.in("room-101").disconnectSockets();
261+
*
262+
* @param room - a room, or an array of rooms
263+
* @return a new {@link BroadcastOperator} instance for chaining
188264
*/
189265
public in(room: Room | Room[]) {
190266
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).in(room);
@@ -193,9 +269,20 @@ export class Namespace<
193269
/**
194270
* Excludes a room when emitting.
195271
*
196-
* @param room
197-
* @return self
198-
* @public
272+
* @example
273+
* const myNamespace = io.of("/my-namespace");
274+
*
275+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
276+
* myNamespace.except("room-101").emit("foo", "bar");
277+
*
278+
* // with an array of rooms
279+
* myNamespace.except(["room-101", "room-102"]).emit("foo", "bar");
280+
*
281+
* // with multiple chained calls
282+
* myNamespace.except("room-101").except("room-102").emit("foo", "bar");
283+
*
284+
* @param room - a room, or an array of rooms
285+
* @return a new {@link BroadcastOperator} instance for chaining
199286
*/
200287
public except(room: Room | Room[]) {
201288
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).except(
@@ -271,10 +358,26 @@ export class Namespace<
271358
}
272359

273360
/**
274-
* Emits to all clients.
361+
* Emits to all connected clients.
362+
*
363+
* @example
364+
* const myNamespace = io.of("/my-namespace");
365+
*
366+
* myNamespace.emit("hello", "world");
367+
*
368+
* // all serializable datastructures are supported (no need to call JSON.stringify)
369+
* myNamespace.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
370+
*
371+
* // with an acknowledgement from the clients
372+
* myNamespace.timeout(1000).emit("some-event", (err, responses) => {
373+
* if (err) {
374+
* // some clients did not acknowledge the event in the given delay
375+
* } else {
376+
* console.log(responses); // one response per client
377+
* }
378+
* });
275379
*
276380
* @return Always true
277-
* @public
278381
*/
279382
public emit<Ev extends EventNames<EmitEvents>>(
280383
ev: Ev,
@@ -289,31 +392,62 @@ export class Namespace<
289392
/**
290393
* Sends a `message` event to all clients.
291394
*
395+
* This method mimics the WebSocket.send() method.
396+
*
397+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
398+
*
399+
* @example
400+
* const myNamespace = io.of("/my-namespace");
401+
*
402+
* myNamespace.send("hello");
403+
*
404+
* // this is equivalent to
405+
* myNamespace.emit("message", "hello");
406+
*
292407
* @return self
293-
* @public
294408
*/
295409
public send(...args: EventParams<EmitEvents, "message">): this {
296410
this.emit("message", ...args);
297411
return this;
298412
}
299413

300414
/**
301-
* Sends a `message` event to all clients.
415+
* Sends a `message` event to all clients. Sends a `message` event. Alias of {@link send}.
302416
*
303417
* @return self
304-
* @public
305418
*/
306419
public write(...args: EventParams<EmitEvents, "message">): this {
307420
this.emit("message", ...args);
308421
return this;
309422
}
310423

311424
/**
312-
* Emit a packet to other Socket.IO servers
425+
* Sends a message to the other Socket.IO servers of the cluster.
426+
*
427+
* @example
428+
* const myNamespace = io.of("/my-namespace");
429+
*
430+
* myNamespace.serverSideEmit("hello", "world");
431+
*
432+
* myNamespace.on("hello", (arg1) => {
433+
* console.log(arg1); // prints "world"
434+
* });
435+
*
436+
* // acknowledgements (without binary content) are supported too:
437+
* myNamespace.serverSideEmit("ping", (err, responses) => {
438+
* if (err) {
439+
* // some clients did not acknowledge the event in the given delay
440+
* } else {
441+
* console.log(responses); // one response per client
442+
* }
443+
* });
444+
*
445+
* myNamespace.on("ping", (cb) => {
446+
* cb("pong");
447+
* });
313448
*
314449
* @param ev - the event name
315450
* @param args - an array of arguments, which may include an acknowledgement callback at the end
316-
* @public
317451
*/
318452
public serverSideEmit<Ev extends EventNames<ServerSideEvents>>(
319453
ev: Ev,
@@ -343,8 +477,6 @@ export class Namespace<
343477
*
344478
* @deprecated this method will be removed in the next major release, please use {@link Namespace#serverSideEmit} or
345479
* {@link Namespace#fetchSockets} instead.
346-
*
347-
* @public
348480
*/
349481
public allSockets(): Promise<Set<SocketId>> {
350482
return new BroadcastOperator<EmitEvents, SocketData>(
@@ -355,9 +487,13 @@ export class Namespace<
355487
/**
356488
* Sets the compress flag.
357489
*
490+
* @example
491+
* const myNamespace = io.of("/my-namespace");
492+
*
493+
* myNamespace.compress(false).emit("hello");
494+
*
358495
* @param compress - if `true`, compresses the sending data
359496
* @return self
360-
* @public
361497
*/
362498
public compress(compress: boolean) {
363499
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).compress(
@@ -370,8 +506,12 @@ export class Namespace<
370506
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
371507
* and is in the middle of a request-response cycle).
372508
*
509+
* @example
510+
* const myNamespace = io.of("/my-namespace");
511+
*
512+
* myNamespace.volatile.emit("hello"); // the clients may or may not receive it
513+
*
373514
* @return self
374-
* @public
375515
*/
376516
public get volatile() {
377517
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).volatile;
@@ -380,24 +520,32 @@ export class Namespace<
380520
/**
381521
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
382522
*
383-
* @return self
384-
* @public
523+
* @example
524+
* const myNamespace = io.of("/my-namespace");
525+
*
526+
* // the “foo” event will be broadcast to all connected clients on this node
527+
* myNamespace.local.emit("foo", "bar");
528+
*
529+
* @return a new {@link BroadcastOperator} instance for chaining
385530
*/
386531
public get local() {
387532
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).local;
388533
}
389534

390535
/**
391-
* Adds a timeout in milliseconds for the next operation
536+
* Adds a timeout in milliseconds for the next operation.
392537
*
393-
* <pre><code>
538+
* @example
539+
* const myNamespace = io.of("/my-namespace");
394540
*
395-
* io.timeout(1000).emit("some-event", (err, responses) => {
396-
* // ...
541+
* myNamespace.timeout(1000).emit("some-event", (err, responses) => {
542+
* if (err) {
543+
* // some clients did not acknowledge the event in the given delay
544+
* } else {
545+
* console.log(responses); // one response per client
546+
* }
397547
* });
398548
*
399-
* </pre></code>
400-
*
401549
* @param timeout
402550
*/
403551
public timeout(timeout: number) {
@@ -407,9 +555,30 @@ export class Namespace<
407555
}
408556

409557
/**
410-
* Returns the matching socket instances
558+
* Returns the matching socket instances.
559+
*
560+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
411561
*
412-
* @public
562+
* @example
563+
* const myNamespace = io.of("/my-namespace");
564+
*
565+
* // return all Socket instances
566+
* const sockets = await myNamespace.fetchSockets();
567+
*
568+
* // return all Socket instances in the "room1" room
569+
* const sockets = await myNamespace.in("room1").fetchSockets();
570+
*
571+
* for (const socket of sockets) {
572+
* console.log(socket.id);
573+
* console.log(socket.handshake);
574+
* console.log(socket.rooms);
575+
* console.log(socket.data);
576+
*
577+
* socket.emit("hello");
578+
* socket.join("room1");
579+
* socket.leave("room2");
580+
* socket.disconnect();
581+
* }
413582
*/
414583
public fetchSockets() {
415584
return new BroadcastOperator<EmitEvents, SocketData>(
@@ -418,10 +587,20 @@ export class Namespace<
418587
}
419588

420589
/**
421-
* Makes the matching socket instances join the specified rooms
590+
* Makes the matching socket instances join the specified rooms.
591+
*
592+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
593+
*
594+
* @example
595+
* const myNamespace = io.of("/my-namespace");
596+
*
597+
* // make all socket instances join the "room1" room
598+
* myNamespace.socketsJoin("room1");
599+
*
600+
* // make all socket instances in the "room1" room join the "room2" and "room3" rooms
601+
* myNamespace.in("room1").socketsJoin(["room2", "room3"]);
422602
*
423-
* @param room
424-
* @public
603+
* @param room - a room, or an array of rooms
425604
*/
426605
public socketsJoin(room: Room | Room[]) {
427606
return new BroadcastOperator<EmitEvents, SocketData>(
@@ -430,10 +609,20 @@ export class Namespace<
430609
}
431610

432611
/**
433-
* Makes the matching socket instances leave the specified rooms
612+
* Makes the matching socket instances leave the specified rooms.
434613
*
435-
* @param room
436-
* @public
614+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
615+
*
616+
* @example
617+
* const myNamespace = io.of("/my-namespace");
618+
*
619+
* // make all socket instances leave the "room1" room
620+
* myNamespace.socketsLeave("room1");
621+
*
622+
* // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
623+
* myNamespace.in("room1").socketsLeave(["room2", "room3"]);
624+
*
625+
* @param room - a room, or an array of rooms
437626
*/
438627
public socketsLeave(room: Room | Room[]) {
439628
return new BroadcastOperator<EmitEvents, SocketData>(
@@ -442,10 +631,20 @@ export class Namespace<
442631
}
443632

444633
/**
445-
* Makes the matching socket instances disconnect
634+
* Makes the matching socket instances disconnect.
635+
*
636+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
637+
*
638+
* @example
639+
* const myNamespace = io.of("/my-namespace");
640+
*
641+
* // make all socket instances disconnect (the connections might be kept alive for other namespaces)
642+
* myNamespace.disconnectSockets();
643+
*
644+
* // make all socket instances in the "room1" room disconnect and close the underlying connections
645+
* myNamespace.in("room1").disconnectSockets(true);
446646
*
447647
* @param close - whether to close the underlying connection
448-
* @public
449648
*/
450649
public disconnectSockets(close: boolean = false) {
451650
return new BroadcastOperator<EmitEvents, SocketData>(

‎lib/socket.ts

+268-85
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,37 @@ export type Event = [string, ...any[]];
128128

129129
function noop() {}
130130

131+
/**
132+
* This is the main object for interacting with a client.
133+
*
134+
* A Socket belongs to a given {@link Namespace} and uses an underlying {@link Client} to communicate.
135+
*
136+
* Within each {@link Namespace}, you can also define arbitrary channels (called "rooms") that the {@link Socket} can
137+
* join and leave. That provides a convenient way to broadcast to a group of socket instances.
138+
*
139+
* @example
140+
* io.on("connection", (socket) => {
141+
* console.log(`socket ${socket.id} connected`);
142+
*
143+
* // send an event to the client
144+
* socket.emit("foo", "bar");
145+
*
146+
* socket.on("foobar", () => {
147+
* // an event was received from the client
148+
* });
149+
*
150+
* // join the room named "room1"
151+
* socket.join("room1");
152+
*
153+
* // broadcast to everyone in the room named "room1"
154+
* io.to("room1").emit("hello");
155+
*
156+
* // upon disconnection
157+
* socket.on("disconnect", (reason) => {
158+
* console.log(`socket ${socket.id} disconnected due to ${reason}`);
159+
* });
160+
* });
161+
*/
131162
export class Socket<
132163
ListenEvents extends EventsMap = DefaultEventsMap,
133164
EmitEvents extends EventsMap = ListenEvents,
@@ -138,13 +169,32 @@ export class Socket<
138169
EmitEvents,
139170
SocketReservedEventsMap
140171
> {
172+
/**
173+
* An unique identifier for the session.
174+
*/
141175
public readonly id: SocketId;
176+
/**
177+
* The handshake details.
178+
*/
142179
public readonly handshake: Handshake;
143180
/**
144-
* Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method
181+
* Additional information that can be attached to the Socket instance and which will be used in the
182+
* {@link Server.fetchSockets()} method.
145183
*/
146184
public data: Partial<SocketData> = {};
147-
185+
/**
186+
* Whether the socket is currently connected or not.
187+
*
188+
* @example
189+
* io.use((socket, next) => {
190+
* console.log(socket.connected); // false
191+
* next();
192+
* });
193+
*
194+
* io.on("connection", (socket) => {
195+
* console.log(socket.connected); // true
196+
* });
197+
*/
148198
public connected: boolean = false;
149199

150200
private readonly server: Server<
@@ -209,8 +259,20 @@ export class Socket<
209259
/**
210260
* Emits to this client.
211261
*
262+
* @example
263+
* io.on("connection", (socket) => {
264+
* socket.emit("hello", "world");
265+
*
266+
* // all serializable datastructures are supported (no need to call JSON.stringify)
267+
* socket.emit("hello", 1, "2", { 3: ["4"], 5: Buffer.from([6]) });
268+
*
269+
* // with an acknowledgement from the client
270+
* socket.emit("hello", "world", (val) => {
271+
* // ...
272+
* });
273+
* });
274+
*
212275
* @return Always returns `true`.
213-
* @public
214276
*/
215277
public emit<Ev extends EventNames<EmitEvents>>(
216278
ev: Ev,
@@ -268,54 +330,93 @@ export class Socket<
268330
/**
269331
* Targets a room when broadcasting.
270332
*
271-
* @param room
272-
* @return self
273-
* @public
333+
* @example
334+
* io.on("connection", (socket) => {
335+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room, except this socket
336+
* socket.to("room-101").emit("foo", "bar");
337+
*
338+
* // the code above is equivalent to:
339+
* io.to("room-101").except(socket.id).emit("foo", "bar");
340+
*
341+
* // with an array of rooms (a client will be notified at most once)
342+
* socket.to(["room-101", "room-102"]).emit("foo", "bar");
343+
*
344+
* // with multiple chained calls
345+
* socket.to("room-101").to("room-102").emit("foo", "bar");
346+
* });
347+
*
348+
* @param room - a room, or an array of rooms
349+
* @return a new {@link BroadcastOperator} instance for chaining
274350
*/
275-
public to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
351+
public to(room: Room | Room[]) {
276352
return this.newBroadcastOperator().to(room);
277353
}
278354

279355
/**
280-
* Targets a room when broadcasting.
356+
* Targets a room when broadcasting. Similar to `to()`, but might feel clearer in some cases:
281357
*
282-
* @param room
283-
* @return self
284-
* @public
358+
* @example
359+
* io.on("connection", (socket) => {
360+
* // disconnect all clients in the "room-101" room, except this socket
361+
* socket.in("room-101").disconnectSockets();
362+
* });
363+
*
364+
* @param room - a room, or an array of rooms
365+
* @return a new {@link BroadcastOperator} instance for chaining
285366
*/
286-
public in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
367+
public in(room: Room | Room[]) {
287368
return this.newBroadcastOperator().in(room);
288369
}
289370

290371
/**
291372
* Excludes a room when broadcasting.
292373
*
293-
* @param room
294-
* @return self
295-
* @public
374+
* @example
375+
* io.on("connection", (socket) => {
376+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
377+
* // and this socket
378+
* socket.except("room-101").emit("foo", "bar");
379+
*
380+
* // with an array of rooms
381+
* socket.except(["room-101", "room-102"]).emit("foo", "bar");
382+
*
383+
* // with multiple chained calls
384+
* socket.except("room-101").except("room-102").emit("foo", "bar");
385+
* });
386+
*
387+
* @param room - a room, or an array of rooms
388+
* @return a new {@link BroadcastOperator} instance for chaining
296389
*/
297-
public except(
298-
room: Room | Room[]
299-
): BroadcastOperator<EmitEvents, SocketData> {
390+
public except(room: Room | Room[]) {
300391
return this.newBroadcastOperator().except(room);
301392
}
302393

303394
/**
304395
* Sends a `message` event.
305396
*
397+
* This method mimics the WebSocket.send() method.
398+
*
399+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
400+
*
401+
* @example
402+
* io.on("connection", (socket) => {
403+
* socket.send("hello");
404+
*
405+
* // this is equivalent to
406+
* socket.emit("message", "hello");
407+
* });
408+
*
306409
* @return self
307-
* @public
308410
*/
309411
public send(...args: EventParams<EmitEvents, "message">): this {
310412
this.emit("message", ...args);
311413
return this;
312414
}
313415

314416
/**
315-
* Sends a `message` event.
417+
* Sends a `message` event. Alias of {@link send}.
316418
*
317419
* @return self
318-
* @public
319420
*/
320421
public write(...args: EventParams<EmitEvents, "message">): this {
321422
this.emit("message", ...args);
@@ -341,9 +442,17 @@ export class Socket<
341442
/**
342443
* Joins a room.
343444
*
445+
* @example
446+
* io.on("connection", (socket) => {
447+
* // join a single room
448+
* socket.join("room1");
449+
*
450+
* // join multiple rooms
451+
* socket.join(["room1", "room2"]);
452+
* });
453+
*
344454
* @param {String|Array} rooms - room or array of rooms
345455
* @return a Promise or nothing, depending on the adapter
346-
* @public
347456
*/
348457
public join(rooms: Room | Array<Room>): Promise<void> | void {
349458
debug("join room %s", rooms);
@@ -357,9 +466,17 @@ export class Socket<
357466
/**
358467
* Leaves a room.
359468
*
469+
* @example
470+
* io.on("connection", (socket) => {
471+
* // leave a single room
472+
* socket.leave("room1");
473+
*
474+
* // leave multiple rooms
475+
* socket.leave("room1").leave("room2");
476+
* });
477+
*
360478
* @param {String} room
361479
* @return a Promise or nothing, depending on the adapter
362-
* @public
363480
*/
364481
public leave(room: string): Promise<void> | void {
365482
debug("leave room %s", room);
@@ -559,10 +676,17 @@ export class Socket<
559676
/**
560677
* Disconnects this client.
561678
*
562-
* @param {Boolean} close - if `true`, closes the underlying connection
563-
* @return {Socket} self
679+
* @example
680+
* io.on("connection", (socket) => {
681+
* // disconnect this socket (the connection might be kept alive for other namespaces)
682+
* socket.disconnect();
683+
*
684+
* // disconnect this socket and close the underlying connection
685+
* socket.disconnect(true);
686+
* })
564687
*
565-
* @public
688+
* @param {Boolean} close - if `true`, closes the underlying connection
689+
* @return self
566690
*/
567691
public disconnect(close = false): this {
568692
if (!this.connected) return this;
@@ -578,9 +702,13 @@ export class Socket<
578702
/**
579703
* Sets the compress flag.
580704
*
705+
* @example
706+
* io.on("connection", (socket) => {
707+
* socket.compress(false).emit("hello");
708+
* });
709+
*
581710
* @param {Boolean} compress - if `true`, compresses the sending data
582711
* @return {Socket} self
583-
* @public
584712
*/
585713
public compress(compress: boolean): this {
586714
this.flags.compress = compress;
@@ -592,8 +720,12 @@ export class Socket<
592720
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
593721
* and is in the middle of a request-response cycle).
594722
*
723+
* @example
724+
* io.on("connection", (socket) => {
725+
* socket.volatile.emit("hello"); // the client may or may not receive it
726+
* });
727+
*
595728
* @return {Socket} self
596-
* @public
597729
*/
598730
public get volatile(): this {
599731
this.flags.volatile = true;
@@ -604,37 +736,47 @@ export class Socket<
604736
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the
605737
* sender.
606738
*
607-
* @return {Socket} self
608-
* @public
739+
* @example
740+
* io.on("connection", (socket) => {
741+
* // the “foo” event will be broadcast to all connected clients, except this socket
742+
* socket.broadcast.emit("foo", "bar");
743+
* });
744+
*
745+
* @return a new {@link BroadcastOperator} instance for chaining
609746
*/
610-
public get broadcast(): BroadcastOperator<EmitEvents, SocketData> {
747+
public get broadcast() {
611748
return this.newBroadcastOperator();
612749
}
613750

614751
/**
615752
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
616753
*
617-
* @return {Socket} self
618-
* @public
754+
* @example
755+
* io.on("connection", (socket) => {
756+
* // the “foo” event will be broadcast to all connected clients on this node, except this socket
757+
* socket.local.emit("foo", "bar");
758+
* });
759+
*
760+
* @return a new {@link BroadcastOperator} instance for chaining
619761
*/
620-
public get local(): BroadcastOperator<EmitEvents, SocketData> {
762+
public get local() {
621763
return this.newBroadcastOperator().local;
622764
}
623765

624766
/**
625767
* Sets a modifier for a subsequent event emission that the callback will be called with an error when the
626768
* given number of milliseconds have elapsed without an acknowledgement from the client:
627769
*
628-
* ```
629-
* socket.timeout(5000).emit("my-event", (err) => {
630-
* if (err) {
631-
* // the client did not acknowledge the event in the given delay
632-
* }
770+
* @example
771+
* io.on("connection", (socket) => {
772+
* socket.timeout(5000).emit("my-event", (err) => {
773+
* if (err) {
774+
* // the client did not acknowledge the event in the given delay
775+
* }
776+
* });
633777
* });
634-
* ```
635778
*
636779
* @returns self
637-
* @public
638780
*/
639781
public timeout(timeout: number): this {
640782
this.flags.timeout = timeout;
@@ -666,9 +808,25 @@ export class Socket<
666808
/**
667809
* Sets up socket middleware.
668810
*
811+
* @example
812+
* io.on("connection", (socket) => {
813+
* socket.use(([event, ...args], next) => {
814+
* if (isUnauthorized(event)) {
815+
* return next(new Error("unauthorized event"));
816+
* }
817+
* // do not forget to call next
818+
* next();
819+
* });
820+
*
821+
* socket.on("error", (err) => {
822+
* if (err && err.message === "unauthorized event") {
823+
* socket.disconnect();
824+
* }
825+
* });
826+
* });
827+
*
669828
* @param {Function} fn - middleware function (event, next)
670829
* @return {Socket} self
671-
* @public
672830
*/
673831
public use(fn: (event: Event, next: (err?: Error) => void) => void): this {
674832
this.fns.push(fn);
@@ -711,8 +869,6 @@ export class Socket<
711869

712870
/**
713871
* A reference to the request that originated the underlying Engine.IO Socket.
714-
*
715-
* @public
716872
*/
717873
public get request(): IncomingMessage {
718874
return this.client.request;
@@ -721,14 +877,30 @@ export class Socket<
721877
/**
722878
* A reference to the underlying Client transport connection (Engine.IO Socket object).
723879
*
724-
* @public
880+
* @example
881+
* io.on("connection", (socket) => {
882+
* console.log(socket.conn.transport.name); // prints "polling" or "websocket"
883+
*
884+
* socket.conn.once("upgrade", () => {
885+
* console.log(socket.conn.transport.name); // prints "websocket"
886+
* });
887+
* });
725888
*/
726889
public get conn() {
727890
return this.client.conn;
728891
}
729892

730893
/**
731-
* @public
894+
* Returns the rooms the socket is currently in.
895+
*
896+
* @example
897+
* io.on("connection", (socket) => {
898+
* console.log(socket.rooms); // Set { <socket.id> }
899+
*
900+
* socket.join("room1");
901+
*
902+
* console.log(socket.rooms); // Set { <socket.id>, "room1" }
903+
* });
732904
*/
733905
public get rooms(): Set<Room> {
734906
return this.adapter.socketRooms(this.id) || new Set();
@@ -738,8 +910,14 @@ export class Socket<
738910
* Adds a listener that will be fired when any event is received. The event name is passed as the first argument to
739911
* the callback.
740912
*
913+
* @example
914+
* io.on("connection", (socket) => {
915+
* socket.onAny((event, ...args) => {
916+
* console.log(`got event ${event}`);
917+
* });
918+
* });
919+
*
741920
* @param listener
742-
* @public
743921
*/
744922
public onAny(listener: (...args: any[]) => void): this {
745923
this._anyListeners = this._anyListeners || [];
@@ -752,7 +930,6 @@ export class Socket<
752930
* the callback. The listener is added to the beginning of the listeners array.
753931
*
754932
* @param listener
755-
* @public
756933
*/
757934
public prependAny(listener: (...args: any[]) => void): this {
758935
this._anyListeners = this._anyListeners || [];
@@ -763,8 +940,22 @@ export class Socket<
763940
/**
764941
* Removes the listener that will be fired when any event is received.
765942
*
943+
* @example
944+
* io.on("connection", (socket) => {
945+
* const catchAllListener = (event, ...args) => {
946+
* console.log(`got event ${event}`);
947+
* }
948+
*
949+
* socket.onAny(catchAllListener);
950+
*
951+
* // remove a specific listener
952+
* socket.offAny(catchAllListener);
953+
*
954+
* // or remove all listeners
955+
* socket.offAny();
956+
* });
957+
*
766958
* @param listener
767-
* @public
768959
*/
769960
public offAny(listener?: (...args: any[]) => void): this {
770961
if (!this._anyListeners) {
@@ -787,28 +978,25 @@ export class Socket<
787978
/**
788979
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
789980
* e.g. to remove listeners.
790-
*
791-
* @public
792981
*/
793982
public listenersAny() {
794983
return this._anyListeners || [];
795984
}
796985

797986
/**
798-
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
799-
* callback.
800-
*
801-
* @param listener
987+
* Adds a listener that will be fired when any event is sent. The event name is passed as the first argument to
988+
* the callback.
802989
*
803-
* <pre><code>
990+
* Note: acknowledgements sent to the client are not included.
804991
*
805-
* socket.onAnyOutgoing((event, ...args) => {
806-
* console.log(event);
992+
* @example
993+
* io.on("connection", (socket) => {
994+
* socket.onAnyOutgoing((event, ...args) => {
995+
* console.log(`sent event ${event}`);
996+
* });
807997
* });
808998
*
809-
* </pre></code>
810-
*
811-
* @public
999+
* @param listener
8121000
*/
8131001
public onAnyOutgoing(listener: (...args: any[]) => void): this {
8141002
this._anyOutgoingListeners = this._anyOutgoingListeners || [];
@@ -820,17 +1008,14 @@ export class Socket<
8201008
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
8211009
* callback. The listener is added to the beginning of the listeners array.
8221010
*
823-
* @param listener
824-
*
825-
* <pre><code>
826-
*
827-
* socket.prependAnyOutgoing((event, ...args) => {
828-
* console.log(event);
1011+
* @example
1012+
* io.on("connection", (socket) => {
1013+
* socket.prependAnyOutgoing((event, ...args) => {
1014+
* console.log(`sent event ${event}`);
1015+
* });
8291016
* });
8301017
*
831-
* </pre></code>
832-
*
833-
* @public
1018+
* @param listener
8341019
*/
8351020
public prependAnyOutgoing(listener: (...args: any[]) => void): this {
8361021
this._anyOutgoingListeners = this._anyOutgoingListeners || [];
@@ -839,24 +1024,24 @@ export class Socket<
8391024
}
8401025

8411026
/**
842-
* Removes the listener that will be fired when any event is emitted.
1027+
* Removes the listener that will be fired when any event is sent.
8431028
*
844-
* @param listener
845-
*
846-
* <pre><code>
847-
*
848-
* const handler = (event, ...args) => {
849-
* console.log(event);
850-
* }
1029+
* @example
1030+
* io.on("connection", (socket) => {
1031+
* const catchAllListener = (event, ...args) => {
1032+
* console.log(`sent event ${event}`);
1033+
* }
8511034
*
852-
* socket.onAnyOutgoing(handler);
1035+
* socket.onAnyOutgoing(catchAllListener);
8531036
*
854-
* // then later
855-
* socket.offAnyOutgoing(handler);
1037+
* // remove a specific listener
1038+
* socket.offAnyOutgoing(catchAllListener);
8561039
*
857-
* </pre></code>
1040+
* // or remove all listeners
1041+
* socket.offAnyOutgoing();
1042+
* });
8581043
*
859-
* @public
1044+
* @param listener - the catch-all listener
8601045
*/
8611046
public offAnyOutgoing(listener?: (...args: any[]) => void): this {
8621047
if (!this._anyOutgoingListeners) {
@@ -879,8 +1064,6 @@ export class Socket<
8791064
/**
8801065
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
8811066
* e.g. to remove listeners.
882-
*
883-
* @public
8841067
*/
8851068
public listenersAnyOutgoing() {
8861069
return this._anyOutgoingListeners || [];

0 commit comments

Comments
 (0)
Please sign in to comment.