MCPcopy
hub / github.com/socketio/socket.io / emit

Method emit

packages/socket.io/lib/broadcast-operator.ts:209–309  ·  view source on GitHub ↗

* Emits to all clients. * * @example * // the “foo” event will be broadcast to all connected clients * io.emit("foo", "bar"); * * // the “foo” event will be broadcast to all connected clients in the “room-101” room * io.to("room-101").emit("foo", "bar"); * * // with an ack

(
    ev: Ev,
    ...args: EventParams<EmitEvents, Ev>
  )

Source from the content-addressed store, hash-verified

207 * @return Always true
208 */
209 public emit<Ev extends EventNames<EmitEvents>>(
210 ev: Ev,
211 ...args: EventParams<EmitEvents, Ev>
212 ): boolean {
213 if (RESERVED_EVENTS.has(ev)) {
214 throw new Error(`"${String(ev)}" is a reserved event name`);
215 }
216 // set up packet object
217 const data = [ev, ...args];
218 const packet = {
219 type: PacketType.EVENT,
220 data: data,
221 };
222
223 const withAck = typeof data[data.length - 1] === "function";
224
225 if (!withAck) {
226 this.adapter.broadcast(packet, {
227 rooms: this.rooms,
228 except: this.exceptRooms,
229 flags: this.flags,
230 });
231
232 return true;
233 }
234
235 const ack = data.pop() as (...args: any[]) => void;
236 let timedOut = false;
237 let responses: any[] = [];
238
239 const timer = setTimeout(() => {
240 timedOut = true;
241
242 debug("operation has timed out");
243 // @ts-expect-error
244 const packetId = packet.id;
245
246 if (packetId !== undefined) {
247 this.adapter.nsp.sockets.forEach((socket) => {
248 socket.acks.delete(packetId);
249 });
250 }
251
252 ack.apply(this, [
253 new Error("operation has timed out"),
254 this.flags.expectSingleResponse ? null : responses,
255 ]);
256 }, this.flags.timeout);
257
258 let expectedServerCount = -1;
259 let actualServerCount = 0;
260 let expectedClientCount = 0;
261
262 const checkCompleteness = () => {
263 debug(
264 "responses: servers: %d / %d ; clients: %d / %d",
265 actualServerCount,
266 expectedServerCount,

Callers 1

emitWithAckMethod · 0.95

Calls 5

debugFunction · 0.85
applyMethod · 0.80
broadcastWithAckMethod · 0.80
serverCountMethod · 0.80
broadcastMethod · 0.45

Tested by

no test coverage detected