MCPcopy Index your code
hub / github.com/socketio/socket.io / emit

Method emit

lib/socket.ts:328–367  ·  view source on GitHub ↗

* Emits to this client. * * @example * io.on("connection", (socket) => { * socket.emit("hello", "world"); * * // all serializable datastructures are supported (no need to call JSON.stringify) * socket.emit("hello", 1, "2", { 3: ["4"], 5: Buffer.from([6]) }); * * //

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

Source from the content-addressed store, hash-verified

326 * @return Always returns `true`.
327 */
328 public emit<Ev extends EventNames<EmitEvents>>(
329 ev: Ev,
330 ...args: EventParams<EmitEvents, Ev>
331 ): boolean {
332 if (RESERVED_EVENTS.has(ev)) {
333 throw new Error(`"${String(ev)}" is a reserved event name`);
334 }
335 const data: any[] = [ev, ...args];
336 const packet: any = {
337 type: PacketType.EVENT,
338 data: data,
339 };
340
341 // access last argument to see if it's an ACK callback
342 if (typeof data[data.length - 1] === "function") {
343 const id = this.nsp._ids++;
344 debug("emitting packet with ack id %d", id);
345
346 this.registerAckCallback(id, data.pop());
347 packet.id = id;
348 }
349
350 const flags = Object.assign({}, this.flags);
351 this.flags = {};
352
353 // @ts-ignore
354 if (this.nsp.server.opts.connectionStateRecovery) {
355 // this ensures the packet is stored and can be transmitted upon reconnection
356 this.adapter.broadcast(packet, {
357 rooms: new Set([this.id]),
358 except: new Set(),
359 flags,
360 });
361 } else {
362 this.notifyOutgoingListeners(packet);
363 this.packet(packet, flags);
364 }
365
366 return true;
367 }
368
369 /**
370 * Emits an event and waits for an acknowledgement

Callers 3

emitWithAckMethod · 0.95
sendMethod · 0.95
writeMethod · 0.95

Calls 4

registerAckCallbackMethod · 0.95
packetMethod · 0.95
broadcastMethod · 0.45

Tested by

no test coverage detected