Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

broadcast-operator.d.ts 8.8KB

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