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

Function fetchSockets

packages/socket.io-adapter/lib/cluster-adapter.ts:570–614  ·  view source on GitHub ↗
(opts: BroadcastOptions)

Source from the content-addressed store, hash-verified

568 }
569
570 async fetchSockets(opts: BroadcastOptions): Promise<any[]> {
571 const [localSockets, serverCount] = await Promise.all([
572 super.fetchSockets(opts),
573 this.serverCount(),
574 ]);
575 const expectedResponseCount = serverCount - 1;
576
577 if (opts.flags?.local || expectedResponseCount <= 0) {
578 return localSockets;
579 }
580
581 const requestId = randomId();
582
583 return new Promise((resolve, reject) => {
584 const timeout = setTimeout(() => {
585 const storedRequest = this.requests.get(requestId);
586 if (storedRequest) {
587 reject(
588 new Error(
589 `timeout reached: only ${storedRequest.current} responses received out of ${storedRequest.expected}`,
590 ),
591 );
592 this.requests.delete(requestId);
593 }
594 }, opts.flags.timeout || DEFAULT_TIMEOUT);
595
596 const storedRequest = {
597 type: MessageType.FETCH_SOCKETS,
598 resolve,
599 timeout,
600 current: 0,
601 expected: expectedResponseCount,
602 responses: localSockets,
603 };
604 this.requests.set(requestId, storedRequest);
605
606 this.publish({
607 type: MessageType.FETCH_SOCKETS,
608 data: {
609 opts: encodeOptions(opts),
610 requestId,
611 },
612 });
613 });
614 }
615
616 override async serverSideEmit(packet: any[]) {
617 const withAck = typeof packet[packet.length - 1] === "function";

Callers

nothing calls this directly

Calls 6

encodeOptionsFunction · 0.85
serverCountMethod · 0.80
keysMethod · 0.80
randomIdFunction · 0.70
fetchSocketsMethod · 0.45
publishMethod · 0.45

Tested by

no test coverage detected