1 | import type { BroadcastFlags, Room, SocketId } from "socket.io-adapter";
|
2 | import { Handshake } from "./socket-types";
|
3 | import type { Adapter } from "socket.io-adapter";
|
4 | import type { EventParams, EventNames, EventsMap, TypedEventBroadcaster, DecorateAcknowledgements, AllButLast, Last, FirstNonErrorArg, EventNamesWithError } from "./typed-events";
|
5 | export declare class BroadcastOperator<EmitEvents extends EventsMap, SocketData> implements TypedEventBroadcaster<EmitEvents> {
|
6 | private readonly adapter;
|
7 | private readonly rooms;
|
8 | private readonly exceptRooms;
|
9 | private readonly flags;
|
10 | constructor(adapter: Adapter, rooms?: Set<Room>, exceptRooms?: Set<Room>, flags?: BroadcastFlags & {
|
11 | expectSingleResponse?: boolean;
|
12 | });
|
13 | /**
|
14 | * Targets a room when emitting.
|
15 | *
|
16 | * @example
|
17 | * // the “foo” event will be broadcast to all connected clients in the “room-101” room
|
18 | * io.to("room-101").emit("foo", "bar");
|
19 | *
|
20 | * // with an array of rooms (a client will be notified at most once)
|
21 | * io.to(["room-101", "room-102"]).emit("foo", "bar");
|
22 | *
|
23 | * // with multiple chained calls
|
24 | * io.to("room-101").to("room-102").emit("foo", "bar");
|
25 | *
|
26 | * @param room - a room, or an array of rooms
|
27 | * @return a new {for chaining
BroadcastOperator} instance |
28 | */
|
29 | to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
|
30 | /**
|
31 | * Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
|
32 | *
|
33 | * @example
|
34 | * // disconnect all clients in the "room-101" room
|
35 | * io.in("room-101").disconnectSockets();
|
36 | *
|
37 | * @param room - a room, or an array of rooms
|
38 | * @return a new {@link BroadcastOperator} instance for chaining
|
39 | */
|
40 | in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
|
41 | /**
|
42 | * Excludes a room when emitting.
|
43 | *
|
44 | * @example
|
45 | * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
|
46 | * io.except("room-101").emit("foo", "bar");
|
47 | *
|
48 | * // with an array of rooms
|
49 | * io.except(["room-101", "room-102"]).emit("foo", "bar");
|
50 | *
|
51 | * // with multiple chained calls
|
52 | * io.except("room-101").except("room-102").emit("foo", "bar");
|
53 | *
|
54 | * @param room - a room, or an array of rooms
|
55 | * @return a new {@link BroadcastOperator} instance for chaining
|
56 | */
|
57 | except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
|
58 | /**
|
59 | * Sets the compress flag.
|
60 | *
|
61 | * @example
|
62 | * io.compress(false).emit("hello");
|
63 | *
|
64 | * @param compress - if `true`, compresses the sending data
|
65 | * @return a new BroadcastOperator instance
|
66 | */
|
67 | compress(compress: boolean): BroadcastOperator<EmitEvents, SocketData>;
|
68 | /**
|
69 | * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
70 | * receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
71 | * and is in the middle of a request-response cycle).
|
72 | *
|
73 | * @example
|
74 | * io.volatile.emit("hello"); // the clients may or may not receive it
|
75 | *
|
76 | * @return a new BroadcastOperator instance
|
77 | */
|
78 | get volatile(): BroadcastOperator<EmitEvents, SocketData>;
|
79 | /**
|
80 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
81 | *
|
82 | * @example
|
83 | * // the “foo” event will be broadcast to all connected clients on this node
|
84 | * io.local.emit("foo", "bar");
|
85 | *
|
86 | * @return a new {@link BroadcastOperator} instance for chaining
|
87 | */
|
88 | get local(): BroadcastOperator<EmitEvents, SocketData>;
|
89 | /**
|
90 | * Adds a timeout in milliseconds for the next operation
|
91 | *
|
92 | * @example
|
93 | * io.timeout(1000).emit("some-event", (err, responses) => {
|
94 | * if (err) {
|
95 | * // some clients did not acknowledge the event in the given delay
|
96 | * } else {
|
97 | * console.log(responses); // one response per client
|
98 | * }
|
99 | * });
|
100 | *
|
101 | * @param timeout
|
102 | */
|
103 | timeout(timeout: number): BroadcastOperator<DecorateAcknowledgements<EmitEvents>, SocketData>;
|
104 | /**
|
105 | * Emits to all clients.
|
106 | *
|
107 | * @example
|
108 | * // the “foo” event will be broadcast to all connected clients
|
109 | * io.emit("foo", "bar");
|
110 | *
|
111 | * // the “foo” event will be broadcast to all connected clients in the “room-101” room
|
112 | * io.to("room-101").emit("foo", "bar");
|
113 | *
|
114 | * // with an acknowledgement expected from all connected clients
|
115 | * io.timeout(1000).emit("some-event", (err, responses) => {
|
116 | * if (err) {
|
117 | * // some clients did not acknowledge the event in the given delay
|
118 | * } else {
|
119 | * console.log(responses); // one response per client
|
120 | * }
|
121 | * });
|
122 | *
|
123 | * @return Always true
|
124 | */
|
125 | emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
|
126 | /**
|
127 | * Emits an event and waits for an acknowledgement from all clients.
|
128 | *
|
129 | * @example
|
130 | * try {
|
131 | * const responses = await io.timeout(1000).emitWithAck("some-event");
|
132 | * console.log(responses); // one response per client
|
133 | * } catch (e) {
|
134 | * // some clients did not acknowledge the event in the given delay
|
135 | * }
|
136 | *
|
137 | * @return a Promise that will be fulfilled when all clients have acknowledged the event
|
138 | */
|
139 | emitWithAck<Ev extends EventNamesWithError<EmitEvents>>(ev: Ev, ...args: AllButLast<EventParams<EmitEvents, Ev>>): Promise<FirstNonErrorArg<Last<EventParams<EmitEvents, Ev>>>>;
|
140 | /**
|
141 | * Gets a list of clients.
|
142 | *
|
143 | * @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
|
144 | * {@link fetchSockets} instead.
|
145 | */
|
146 | allSockets(): Promise<Set<SocketId>>;
|
147 | /**
|
148 | * Returns the matching socket instances. This method works across a cluster of several Socket.IO servers.
|
149 | *
|
150 | * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
151 | *
|
152 | * @example
|
153 | * // return all Socket instances
|
154 | * const sockets = await io.fetchSockets();
|
155 | *
|
156 | * // return all Socket instances in the "room1" room
|
157 | * const sockets = await io.in("room1").fetchSockets();
|
158 | *
|
159 | * for (const socket of sockets) {
|
160 | * console.log(socket.id);
|
161 | * console.log(socket.handshake);
|
162 | * console.log(socket.rooms);
|
163 | * console.log(socket.data);
|
164 | *
|
165 | * socket.emit("hello");
|
166 | * socket.join("room1");
|
167 | * socket.leave("room2");
|
168 | * socket.disconnect();
|
169 | * }
|
170 | */
|
171 | fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
|
172 | /**
|
173 | * Makes the matching socket instances join the specified rooms.
|
174 | *
|
175 | * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
176 | *
|
177 | * @example
|
178 | *
|
179 | * // make all socket instances join the "room1" room
|
180 | * io.socketsJoin("room1");
|
181 | *
|
182 | * // make all socket instances in the "room1" room join the "room2" and "room3" rooms
|
183 | * io.in("room1").socketsJoin(["room2", "room3"]);
|
184 | *
|
185 | * @param room - a room, or an array of rooms
|
186 | */
|
187 | socketsJoin(room: Room | Room[]): void;
|
188 | /**
|
189 | * Makes the matching socket instances leave the specified rooms.
|
190 | *
|
191 | * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
192 | *
|
193 | * @example
|
194 | * // make all socket instances leave the "room1" room
|
195 | * io.socketsLeave("room1");
|
196 | *
|
197 | * // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
|
198 | * io.in("room1").socketsLeave(["room2", "room3"]);
|
199 | *
|
200 | * @param room - a room, or an array of rooms
|
201 | */
|
202 | socketsLeave(room: Room | Room[]): void;
|
203 | /**
|
204 | * Makes the matching socket instances disconnect.
|
205 | *
|
206 | * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
207 | *
|
208 | * @example
|
209 | * // make all socket instances disconnect (the connections might be kept alive for other namespaces)
|
210 | * io.disconnectSockets();
|
211 | *
|
212 | * // make all socket instances in the "room1" room disconnect and close the underlying connections
|
213 | * io.in("room1").disconnectSockets(true);
|
214 | *
|
215 | * @param close - whether to close the underlying connection
|
216 | */
|
217 | disconnectSockets(close?: boolean): void;
|
218 | }
|
219 | /**
|
220 | * Format of the data when the Socket instance exists on another Socket.IO server
|
221 | */
|
222 | interface SocketDetails<SocketData> {
|
223 | id: SocketId;
|
224 | handshake: Handshake;
|
225 | rooms: Room[];
|
226 | data: SocketData;
|
227 | }
|
228 | /**
|
229 | * Expose of subset of the attributes and methods of the Socket class
|
230 | */
|
231 | export declare class RemoteSocket<EmitEvents extends EventsMap, SocketData> implements TypedEventBroadcaster<EmitEvents> {
|
232 | readonly id: SocketId;
|
233 | readonly handshake: Handshake;
|
234 | readonly rooms: Set<Room>;
|
235 | readonly data: SocketData;
|
236 | private readonly operator;
|
237 | constructor(adapter: Adapter, details: SocketDetails<SocketData>);
|
238 | /**
|
239 | * Adds a timeout in milliseconds for the next operation.
|
240 | *
|
241 | * @example
|
242 | * const sockets = await io.fetchSockets();
|
243 | *
|
244 | * for (const socket of sockets) {
|
245 | * if (someCondition) {
|
246 | * socket.timeout(1000).emit("some-event", (err) => {
|
247 | * if (err) {
|
248 | * // the client did not acknowledge the event in the given delay
|
249 | * }
|
250 | * });
|
251 | * }
|
252 | * }
|
253 | *
|
254 | * // note: if possible, using a room instead of looping over all sockets is preferable
|
255 | * io.timeout(1000).to(someConditionRoom).emit("some-event", (err, responses) => {
|
256 | * // ...
|
257 | * });
|
258 | *
|
259 | * timeout
|
260 | */
|
261 | timeout(timeout: number): BroadcastOperator<DecorateAcknowledgements<EmitEvents>, SocketData>;
|
262 | emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
|
263 | /**
|
264 | * Joins a room.
|
265 | *
|
266 | * @param {String|Array} room - room or array of rooms
|
267 | */
|
268 | join(room: Room | Room[]): void;
|
269 | /**
|
270 | * Leaves a room.
|
271 | *
|
272 | * @param {String} room
|
273 | */
|
274 | leave(room: Room): void;
|
275 | /**
|
276 | * Disconnects this client.
|
277 | *
|
278 | * @param {Boolean} close - if `true`, closes the underlying connection
|
279 | * @return {Socket} self
|
280 | */
|
281 | disconnect(close?: boolean): this;
|
282 | }
|
283 | export {};
|