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

Function setupPrimary

packages/socket.io-cluster-engine/lib/cluster.ts:12–42  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10function ignoreError() {}
11
12export function setupPrimary() {
13 cluster.on("message", (sourceWorker, message: { _source?: string }) => {
14 if (message._source !== MESSAGE_SOURCE) {
15 debug("ignore message from unknown source");
16 return;
17 }
18
19 if (!sourceWorker[kNodeId]) {
20 sourceWorker[kNodeId] = (message as Message).senderId;
21 }
22
23 // @ts-expect-error recipientId is not defined for all messages
24 let recipientId = (message as Message).recipientId;
25 if (recipientId) {
26 for (const worker of Object.values(cluster.workers)) {
27 if (worker[kNodeId] === recipientId) {
28 debug("forward message to worker %d", worker.id);
29 worker.send(message, null, ignoreError);
30 return;
31 }
32 }
33 }
34
35 debug("forward message to all other workers");
36 for (const worker of Object.values(cluster.workers)) {
37 if (worker.id !== sourceWorker.id) {
38 worker.send(message, null, ignoreError);
39 }
40 }
41 });
42}
43
44export class NodeClusterEngine extends ClusterEngine {
45 constructor(opts?: ServerOptions) {

Callers 1

cluster.tsFile · 0.90

Calls 3

debugFunction · 0.85
onMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected