11cb0ef41Sopenharmony_ci# Cluster
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/cluster.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciClusters of Node.js processes can be used to run multiple instances of Node.js
101cb0ef41Sopenharmony_cithat can distribute workloads among their application threads. When process
111cb0ef41Sopenharmony_ciisolation is not needed, use the [`worker_threads`][] module instead, which
121cb0ef41Sopenharmony_ciallows running multiple application threads within a single Node.js instance.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciThe cluster module allows easy creation of child processes that all share
151cb0ef41Sopenharmony_ciserver ports.
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci```mjs
181cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
191cb0ef41Sopenharmony_ciimport http from 'node:http';
201cb0ef41Sopenharmony_ciimport { availableParallelism } from 'node:os';
211cb0ef41Sopenharmony_ciimport process from 'node:process';
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst numCPUs = availableParallelism();
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
261cb0ef41Sopenharmony_ci  console.log(`Primary ${process.pid} is running`);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  // Fork workers.
291cb0ef41Sopenharmony_ci  for (let i = 0; i < numCPUs; i++) {
301cb0ef41Sopenharmony_ci    cluster.fork();
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  cluster.on('exit', (worker, code, signal) => {
341cb0ef41Sopenharmony_ci    console.log(`worker ${worker.process.pid} died`);
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci} else {
371cb0ef41Sopenharmony_ci  // Workers can share any TCP connection
381cb0ef41Sopenharmony_ci  // In this case it is an HTTP server
391cb0ef41Sopenharmony_ci  http.createServer((req, res) => {
401cb0ef41Sopenharmony_ci    res.writeHead(200);
411cb0ef41Sopenharmony_ci    res.end('hello world\n');
421cb0ef41Sopenharmony_ci  }).listen(8000);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  console.log(`Worker ${process.pid} started`);
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci```
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci```cjs
491cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
501cb0ef41Sopenharmony_ciconst http = require('node:http');
511cb0ef41Sopenharmony_ciconst numCPUs = require('node:os').availableParallelism();
521cb0ef41Sopenharmony_ciconst process = require('node:process');
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
551cb0ef41Sopenharmony_ci  console.log(`Primary ${process.pid} is running`);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  // Fork workers.
581cb0ef41Sopenharmony_ci  for (let i = 0; i < numCPUs; i++) {
591cb0ef41Sopenharmony_ci    cluster.fork();
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  cluster.on('exit', (worker, code, signal) => {
631cb0ef41Sopenharmony_ci    console.log(`worker ${worker.process.pid} died`);
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci} else {
661cb0ef41Sopenharmony_ci  // Workers can share any TCP connection
671cb0ef41Sopenharmony_ci  // In this case it is an HTTP server
681cb0ef41Sopenharmony_ci  http.createServer((req, res) => {
691cb0ef41Sopenharmony_ci    res.writeHead(200);
701cb0ef41Sopenharmony_ci    res.end('hello world\n');
711cb0ef41Sopenharmony_ci  }).listen(8000);
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  console.log(`Worker ${process.pid} started`);
741cb0ef41Sopenharmony_ci}
751cb0ef41Sopenharmony_ci```
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciRunning Node.js will now share port 8000 between the workers:
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci```console
801cb0ef41Sopenharmony_ci$ node server.js
811cb0ef41Sopenharmony_ciPrimary 3596 is running
821cb0ef41Sopenharmony_ciWorker 4324 started
831cb0ef41Sopenharmony_ciWorker 4520 started
841cb0ef41Sopenharmony_ciWorker 6056 started
851cb0ef41Sopenharmony_ciWorker 5644 started
861cb0ef41Sopenharmony_ci```
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciOn Windows, it is not yet possible to set up a named pipe server in a worker.
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci## How it works
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci<!--type=misc-->
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ciThe worker processes are spawned using the [`child_process.fork()`][] method,
951cb0ef41Sopenharmony_ciso that they can communicate with the parent via IPC and pass server
961cb0ef41Sopenharmony_cihandles back and forth.
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciThe cluster module supports two methods of distributing incoming
991cb0ef41Sopenharmony_ciconnections.
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciThe first one (and the default one on all platforms except Windows)
1021cb0ef41Sopenharmony_ciis the round-robin approach, where the primary process listens on a
1031cb0ef41Sopenharmony_ciport, accepts new connections and distributes them across the workers
1041cb0ef41Sopenharmony_ciin a round-robin fashion, with some built-in smarts to avoid
1051cb0ef41Sopenharmony_cioverloading a worker process.
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciThe second approach is where the primary process creates the listen
1081cb0ef41Sopenharmony_cisocket and sends it to interested workers. The workers then accept
1091cb0ef41Sopenharmony_ciincoming connections directly.
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ciThe second approach should, in theory, give the best performance.
1121cb0ef41Sopenharmony_ciIn practice however, distribution tends to be very unbalanced due
1131cb0ef41Sopenharmony_cito operating system scheduler vagaries. Loads have been observed
1141cb0ef41Sopenharmony_ciwhere over 70% of all connections ended up in just two processes,
1151cb0ef41Sopenharmony_ciout of a total of eight.
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ciBecause `server.listen()` hands off most of the work to the primary
1181cb0ef41Sopenharmony_ciprocess, there are three cases where the behavior between a normal
1191cb0ef41Sopenharmony_ciNode.js process and a cluster worker differs:
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci1. `server.listen({fd: 7})` Because the message is passed to the primary,
1221cb0ef41Sopenharmony_ci   file descriptor 7 **in the parent** will be listened on, and the
1231cb0ef41Sopenharmony_ci   handle passed to the worker, rather than listening to the worker's
1241cb0ef41Sopenharmony_ci   idea of what the number 7 file descriptor references.
1251cb0ef41Sopenharmony_ci2. `server.listen(handle)` Listening on handles explicitly will cause
1261cb0ef41Sopenharmony_ci   the worker to use the supplied handle, rather than talk to the primary
1271cb0ef41Sopenharmony_ci   process.
1281cb0ef41Sopenharmony_ci3. `server.listen(0)` Normally, this will cause servers to listen on a
1291cb0ef41Sopenharmony_ci   random port. However, in a cluster, each worker will receive the
1301cb0ef41Sopenharmony_ci   same "random" port each time they do `listen(0)`. In essence, the
1311cb0ef41Sopenharmony_ci   port is random the first time, but predictable thereafter. To listen
1321cb0ef41Sopenharmony_ci   on a unique port, generate a port number based on the cluster worker ID.
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ciNode.js does not provide routing logic. It is therefore important to design an
1351cb0ef41Sopenharmony_ciapplication such that it does not rely too heavily on in-memory data objects for
1361cb0ef41Sopenharmony_cithings like sessions and login.
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciBecause workers are all separate processes, they can be killed or
1391cb0ef41Sopenharmony_cire-spawned depending on a program's needs, without affecting other
1401cb0ef41Sopenharmony_ciworkers. As long as there are some workers still alive, the server will
1411cb0ef41Sopenharmony_cicontinue to accept connections. If no workers are alive, existing connections
1421cb0ef41Sopenharmony_ciwill be dropped and new connections will be refused. Node.js does not
1431cb0ef41Sopenharmony_ciautomatically manage the number of workers, however. It is the application's
1441cb0ef41Sopenharmony_ciresponsibility to manage the worker pool based on its own needs.
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ciAlthough a primary use case for the `node:cluster` module is networking, it can
1471cb0ef41Sopenharmony_cialso be used for other use cases requiring worker processes.
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci## Class: `Worker`
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci<!-- YAML
1521cb0ef41Sopenharmony_ciadded: v0.7.0
1531cb0ef41Sopenharmony_ci-->
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci* Extends: {EventEmitter}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ciA `Worker` object contains all public information and method about a worker.
1581cb0ef41Sopenharmony_ciIn the primary it can be obtained using `cluster.workers`. In a worker
1591cb0ef41Sopenharmony_ciit can be obtained using `cluster.worker`.
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci### Event: `'disconnect'`
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci<!-- YAML
1641cb0ef41Sopenharmony_ciadded: v0.7.7
1651cb0ef41Sopenharmony_ci-->
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ciSimilar to the `cluster.on('disconnect')` event, but specific to this worker.
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci```js
1701cb0ef41Sopenharmony_cicluster.fork().on('disconnect', () => {
1711cb0ef41Sopenharmony_ci  // Worker has disconnected
1721cb0ef41Sopenharmony_ci});
1731cb0ef41Sopenharmony_ci```
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci### Event: `'error'`
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci<!-- YAML
1781cb0ef41Sopenharmony_ciadded: v0.7.3
1791cb0ef41Sopenharmony_ci-->
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ciThis event is the same as the one provided by [`child_process.fork()`][].
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ciWithin a worker, `process.on('error')` may also be used.
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci### Event: `'exit'`
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci<!-- YAML
1881cb0ef41Sopenharmony_ciadded: v0.11.2
1891cb0ef41Sopenharmony_ci-->
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci* `code` {number} The exit code, if it exited normally.
1921cb0ef41Sopenharmony_ci* `signal` {string} The name of the signal (e.g. `'SIGHUP'`) that caused
1931cb0ef41Sopenharmony_ci  the process to be killed.
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ciSimilar to the `cluster.on('exit')` event, but specific to this worker.
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci```mjs
1981cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
2011cb0ef41Sopenharmony_ci  const worker = cluster.fork();
2021cb0ef41Sopenharmony_ci  worker.on('exit', (code, signal) => {
2031cb0ef41Sopenharmony_ci    if (signal) {
2041cb0ef41Sopenharmony_ci      console.log(`worker was killed by signal: ${signal}`);
2051cb0ef41Sopenharmony_ci    } else if (code !== 0) {
2061cb0ef41Sopenharmony_ci      console.log(`worker exited with error code: ${code}`);
2071cb0ef41Sopenharmony_ci    } else {
2081cb0ef41Sopenharmony_ci      console.log('worker success!');
2091cb0ef41Sopenharmony_ci    }
2101cb0ef41Sopenharmony_ci  });
2111cb0ef41Sopenharmony_ci}
2121cb0ef41Sopenharmony_ci```
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci```cjs
2151cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
2181cb0ef41Sopenharmony_ci  const worker = cluster.fork();
2191cb0ef41Sopenharmony_ci  worker.on('exit', (code, signal) => {
2201cb0ef41Sopenharmony_ci    if (signal) {
2211cb0ef41Sopenharmony_ci      console.log(`worker was killed by signal: ${signal}`);
2221cb0ef41Sopenharmony_ci    } else if (code !== 0) {
2231cb0ef41Sopenharmony_ci      console.log(`worker exited with error code: ${code}`);
2241cb0ef41Sopenharmony_ci    } else {
2251cb0ef41Sopenharmony_ci      console.log('worker success!');
2261cb0ef41Sopenharmony_ci    }
2271cb0ef41Sopenharmony_ci  });
2281cb0ef41Sopenharmony_ci}
2291cb0ef41Sopenharmony_ci```
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci### Event: `'listening'`
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci<!-- YAML
2341cb0ef41Sopenharmony_ciadded: v0.7.0
2351cb0ef41Sopenharmony_ci-->
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci* `address` {Object}
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ciSimilar to the `cluster.on('listening')` event, but specific to this worker.
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci```mjs
2421cb0ef41Sopenharmony_cicluster.fork().on('listening', (address) => {
2431cb0ef41Sopenharmony_ci  // Worker is listening
2441cb0ef41Sopenharmony_ci});
2451cb0ef41Sopenharmony_ci```
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ci```cjs
2481cb0ef41Sopenharmony_cicluster.fork().on('listening', (address) => {
2491cb0ef41Sopenharmony_ci  // Worker is listening
2501cb0ef41Sopenharmony_ci});
2511cb0ef41Sopenharmony_ci```
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ciIt is not emitted in the worker.
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci### Event: `'message'`
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_ci<!-- YAML
2581cb0ef41Sopenharmony_ciadded: v0.7.0
2591cb0ef41Sopenharmony_ci-->
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci* `message` {Object}
2621cb0ef41Sopenharmony_ci* `handle` {undefined|Object}
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ciSimilar to the `'message'` event of `cluster`, but specific to this worker.
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ciWithin a worker, `process.on('message')` may also be used.
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ciSee [`process` event: `'message'`][].
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ciHere is an example using the message system. It keeps a count in the primary
2711cb0ef41Sopenharmony_ciprocess of the number of HTTP requests received by the workers:
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ci```mjs
2741cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
2751cb0ef41Sopenharmony_ciimport http from 'node:http';
2761cb0ef41Sopenharmony_ciimport { availableParallelism } from 'node:os';
2771cb0ef41Sopenharmony_ciimport process from 'node:process';
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci  // Keep track of http requests
2821cb0ef41Sopenharmony_ci  let numReqs = 0;
2831cb0ef41Sopenharmony_ci  setInterval(() => {
2841cb0ef41Sopenharmony_ci    console.log(`numReqs = ${numReqs}`);
2851cb0ef41Sopenharmony_ci  }, 1000);
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci  // Count requests
2881cb0ef41Sopenharmony_ci  function messageHandler(msg) {
2891cb0ef41Sopenharmony_ci    if (msg.cmd && msg.cmd === 'notifyRequest') {
2901cb0ef41Sopenharmony_ci      numReqs += 1;
2911cb0ef41Sopenharmony_ci    }
2921cb0ef41Sopenharmony_ci  }
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci  // Start workers and listen for messages containing notifyRequest
2951cb0ef41Sopenharmony_ci  const numCPUs = availableParallelism();
2961cb0ef41Sopenharmony_ci  for (let i = 0; i < numCPUs; i++) {
2971cb0ef41Sopenharmony_ci    cluster.fork();
2981cb0ef41Sopenharmony_ci  }
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci  for (const id in cluster.workers) {
3011cb0ef41Sopenharmony_ci    cluster.workers[id].on('message', messageHandler);
3021cb0ef41Sopenharmony_ci  }
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci} else {
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ci  // Worker processes have a http server.
3071cb0ef41Sopenharmony_ci  http.Server((req, res) => {
3081cb0ef41Sopenharmony_ci    res.writeHead(200);
3091cb0ef41Sopenharmony_ci    res.end('hello world\n');
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci    // Notify primary about the request
3121cb0ef41Sopenharmony_ci    process.send({ cmd: 'notifyRequest' });
3131cb0ef41Sopenharmony_ci  }).listen(8000);
3141cb0ef41Sopenharmony_ci}
3151cb0ef41Sopenharmony_ci```
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci```cjs
3181cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
3191cb0ef41Sopenharmony_ciconst http = require('node:http');
3201cb0ef41Sopenharmony_ciconst process = require('node:process');
3211cb0ef41Sopenharmony_ci
3221cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_ci  // Keep track of http requests
3251cb0ef41Sopenharmony_ci  let numReqs = 0;
3261cb0ef41Sopenharmony_ci  setInterval(() => {
3271cb0ef41Sopenharmony_ci    console.log(`numReqs = ${numReqs}`);
3281cb0ef41Sopenharmony_ci  }, 1000);
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ci  // Count requests
3311cb0ef41Sopenharmony_ci  function messageHandler(msg) {
3321cb0ef41Sopenharmony_ci    if (msg.cmd && msg.cmd === 'notifyRequest') {
3331cb0ef41Sopenharmony_ci      numReqs += 1;
3341cb0ef41Sopenharmony_ci    }
3351cb0ef41Sopenharmony_ci  }
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci  // Start workers and listen for messages containing notifyRequest
3381cb0ef41Sopenharmony_ci  const numCPUs = require('node:os').availableParallelism();
3391cb0ef41Sopenharmony_ci  for (let i = 0; i < numCPUs; i++) {
3401cb0ef41Sopenharmony_ci    cluster.fork();
3411cb0ef41Sopenharmony_ci  }
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci  for (const id in cluster.workers) {
3441cb0ef41Sopenharmony_ci    cluster.workers[id].on('message', messageHandler);
3451cb0ef41Sopenharmony_ci  }
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ci} else {
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ci  // Worker processes have a http server.
3501cb0ef41Sopenharmony_ci  http.Server((req, res) => {
3511cb0ef41Sopenharmony_ci    res.writeHead(200);
3521cb0ef41Sopenharmony_ci    res.end('hello world\n');
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci    // Notify primary about the request
3551cb0ef41Sopenharmony_ci    process.send({ cmd: 'notifyRequest' });
3561cb0ef41Sopenharmony_ci  }).listen(8000);
3571cb0ef41Sopenharmony_ci}
3581cb0ef41Sopenharmony_ci```
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ci### Event: `'online'`
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci<!-- YAML
3631cb0ef41Sopenharmony_ciadded: v0.7.0
3641cb0ef41Sopenharmony_ci-->
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ciSimilar to the `cluster.on('online')` event, but specific to this worker.
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci```js
3691cb0ef41Sopenharmony_cicluster.fork().on('online', () => {
3701cb0ef41Sopenharmony_ci  // Worker is online
3711cb0ef41Sopenharmony_ci});
3721cb0ef41Sopenharmony_ci```
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ciIt is not emitted in the worker.
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci### `worker.disconnect()`
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ci<!-- YAML
3791cb0ef41Sopenharmony_ciadded: v0.7.7
3801cb0ef41Sopenharmony_cichanges:
3811cb0ef41Sopenharmony_ci  - version: v7.3.0
3821cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10019
3831cb0ef41Sopenharmony_ci    description: This method now returns a reference to `worker`.
3841cb0ef41Sopenharmony_ci-->
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ci* Returns: {cluster.Worker} A reference to `worker`.
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ciIn a worker, this function will close all servers, wait for the `'close'` event
3891cb0ef41Sopenharmony_cion those servers, and then disconnect the IPC channel.
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_ciIn the primary, an internal message is sent to the worker causing it to call
3921cb0ef41Sopenharmony_ci`.disconnect()` on itself.
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ciCauses `.exitedAfterDisconnect` to be set.
3951cb0ef41Sopenharmony_ci
3961cb0ef41Sopenharmony_ciAfter a server is closed, it will no longer accept new connections,
3971cb0ef41Sopenharmony_cibut connections may be accepted by any other listening worker. Existing
3981cb0ef41Sopenharmony_ciconnections will be allowed to close as usual. When no more connections exist,
3991cb0ef41Sopenharmony_cisee [`server.close()`][], the IPC channel to the worker will close allowing it
4001cb0ef41Sopenharmony_cito die gracefully.
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ciThe above applies _only_ to server connections, client connections are not
4031cb0ef41Sopenharmony_ciautomatically closed by workers, and disconnect does not wait for them to close
4041cb0ef41Sopenharmony_cibefore exiting.
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ciIn a worker, `process.disconnect` exists, but it is not this function;
4071cb0ef41Sopenharmony_ciit is [`disconnect()`][].
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ciBecause long living server connections may block workers from disconnecting, it
4101cb0ef41Sopenharmony_cimay be useful to send a message, so application specific actions may be taken to
4111cb0ef41Sopenharmony_ciclose them. It also may be useful to implement a timeout, killing a worker if
4121cb0ef41Sopenharmony_cithe `'disconnect'` event has not been emitted after some time.
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ci```js
4151cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
4161cb0ef41Sopenharmony_ci  const worker = cluster.fork();
4171cb0ef41Sopenharmony_ci  let timeout;
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ci  worker.on('listening', (address) => {
4201cb0ef41Sopenharmony_ci    worker.send('shutdown');
4211cb0ef41Sopenharmony_ci    worker.disconnect();
4221cb0ef41Sopenharmony_ci    timeout = setTimeout(() => {
4231cb0ef41Sopenharmony_ci      worker.kill();
4241cb0ef41Sopenharmony_ci    }, 2000);
4251cb0ef41Sopenharmony_ci  });
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_ci  worker.on('disconnect', () => {
4281cb0ef41Sopenharmony_ci    clearTimeout(timeout);
4291cb0ef41Sopenharmony_ci  });
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci} else if (cluster.isWorker) {
4321cb0ef41Sopenharmony_ci  const net = require('node:net');
4331cb0ef41Sopenharmony_ci  const server = net.createServer((socket) => {
4341cb0ef41Sopenharmony_ci    // Connections never end
4351cb0ef41Sopenharmony_ci  });
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ci  server.listen(8000);
4381cb0ef41Sopenharmony_ci
4391cb0ef41Sopenharmony_ci  process.on('message', (msg) => {
4401cb0ef41Sopenharmony_ci    if (msg === 'shutdown') {
4411cb0ef41Sopenharmony_ci      // Initiate graceful close of any connections to server
4421cb0ef41Sopenharmony_ci    }
4431cb0ef41Sopenharmony_ci  });
4441cb0ef41Sopenharmony_ci}
4451cb0ef41Sopenharmony_ci```
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ci### `worker.exitedAfterDisconnect`
4481cb0ef41Sopenharmony_ci
4491cb0ef41Sopenharmony_ci<!-- YAML
4501cb0ef41Sopenharmony_ciadded: v6.0.0
4511cb0ef41Sopenharmony_ci-->
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci* {boolean}
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ciThis property is `true` if the worker exited due to `.disconnect()`.
4561cb0ef41Sopenharmony_ciIf the worker exited any other way, it is `false`. If the
4571cb0ef41Sopenharmony_ciworker has not exited, it is `undefined`.
4581cb0ef41Sopenharmony_ci
4591cb0ef41Sopenharmony_ciThe boolean [`worker.exitedAfterDisconnect`][] allows distinguishing between
4601cb0ef41Sopenharmony_civoluntary and accidental exit, the primary may choose not to respawn a worker
4611cb0ef41Sopenharmony_cibased on this value.
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_ci```js
4641cb0ef41Sopenharmony_cicluster.on('exit', (worker, code, signal) => {
4651cb0ef41Sopenharmony_ci  if (worker.exitedAfterDisconnect === true) {
4661cb0ef41Sopenharmony_ci    console.log('Oh, it was just voluntary – no need to worry');
4671cb0ef41Sopenharmony_ci  }
4681cb0ef41Sopenharmony_ci});
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ci// kill worker
4711cb0ef41Sopenharmony_ciworker.kill();
4721cb0ef41Sopenharmony_ci```
4731cb0ef41Sopenharmony_ci
4741cb0ef41Sopenharmony_ci### `worker.id`
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ci<!-- YAML
4771cb0ef41Sopenharmony_ciadded: v0.8.0
4781cb0ef41Sopenharmony_ci-->
4791cb0ef41Sopenharmony_ci
4801cb0ef41Sopenharmony_ci* {integer}
4811cb0ef41Sopenharmony_ci
4821cb0ef41Sopenharmony_ciEach new worker is given its own unique id, this id is stored in the
4831cb0ef41Sopenharmony_ci`id`.
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_ciWhile a worker is alive, this is the key that indexes it in
4861cb0ef41Sopenharmony_ci`cluster.workers`.
4871cb0ef41Sopenharmony_ci
4881cb0ef41Sopenharmony_ci### `worker.isConnected()`
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci<!-- YAML
4911cb0ef41Sopenharmony_ciadded: v0.11.14
4921cb0ef41Sopenharmony_ci-->
4931cb0ef41Sopenharmony_ci
4941cb0ef41Sopenharmony_ciThis function returns `true` if the worker is connected to its primary via its
4951cb0ef41Sopenharmony_ciIPC channel, `false` otherwise. A worker is connected to its primary after it
4961cb0ef41Sopenharmony_cihas been created. It is disconnected after the `'disconnect'` event is emitted.
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci### `worker.isDead()`
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci<!-- YAML
5011cb0ef41Sopenharmony_ciadded: v0.11.14
5021cb0ef41Sopenharmony_ci-->
5031cb0ef41Sopenharmony_ci
5041cb0ef41Sopenharmony_ciThis function returns `true` if the worker's process has terminated (either
5051cb0ef41Sopenharmony_cibecause of exiting or being signaled). Otherwise, it returns `false`.
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci```mjs
5081cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
5091cb0ef41Sopenharmony_ciimport http from 'node:http';
5101cb0ef41Sopenharmony_ciimport { availableParallelism } from 'node:os';
5111cb0ef41Sopenharmony_ciimport process from 'node:process';
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ciconst numCPUs = availableParallelism();
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
5161cb0ef41Sopenharmony_ci  console.log(`Primary ${process.pid} is running`);
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_ci  // Fork workers.
5191cb0ef41Sopenharmony_ci  for (let i = 0; i < numCPUs; i++) {
5201cb0ef41Sopenharmony_ci    cluster.fork();
5211cb0ef41Sopenharmony_ci  }
5221cb0ef41Sopenharmony_ci
5231cb0ef41Sopenharmony_ci  cluster.on('fork', (worker) => {
5241cb0ef41Sopenharmony_ci    console.log('worker is dead:', worker.isDead());
5251cb0ef41Sopenharmony_ci  });
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci  cluster.on('exit', (worker, code, signal) => {
5281cb0ef41Sopenharmony_ci    console.log('worker is dead:', worker.isDead());
5291cb0ef41Sopenharmony_ci  });
5301cb0ef41Sopenharmony_ci} else {
5311cb0ef41Sopenharmony_ci  // Workers can share any TCP connection. In this case, it is an HTTP server.
5321cb0ef41Sopenharmony_ci  http.createServer((req, res) => {
5331cb0ef41Sopenharmony_ci    res.writeHead(200);
5341cb0ef41Sopenharmony_ci    res.end(`Current process\n ${process.pid}`);
5351cb0ef41Sopenharmony_ci    process.kill(process.pid);
5361cb0ef41Sopenharmony_ci  }).listen(8000);
5371cb0ef41Sopenharmony_ci}
5381cb0ef41Sopenharmony_ci```
5391cb0ef41Sopenharmony_ci
5401cb0ef41Sopenharmony_ci```cjs
5411cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
5421cb0ef41Sopenharmony_ciconst http = require('node:http');
5431cb0ef41Sopenharmony_ciconst numCPUs = require('node:os').availableParallelism();
5441cb0ef41Sopenharmony_ciconst process = require('node:process');
5451cb0ef41Sopenharmony_ci
5461cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
5471cb0ef41Sopenharmony_ci  console.log(`Primary ${process.pid} is running`);
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ci  // Fork workers.
5501cb0ef41Sopenharmony_ci  for (let i = 0; i < numCPUs; i++) {
5511cb0ef41Sopenharmony_ci    cluster.fork();
5521cb0ef41Sopenharmony_ci  }
5531cb0ef41Sopenharmony_ci
5541cb0ef41Sopenharmony_ci  cluster.on('fork', (worker) => {
5551cb0ef41Sopenharmony_ci    console.log('worker is dead:', worker.isDead());
5561cb0ef41Sopenharmony_ci  });
5571cb0ef41Sopenharmony_ci
5581cb0ef41Sopenharmony_ci  cluster.on('exit', (worker, code, signal) => {
5591cb0ef41Sopenharmony_ci    console.log('worker is dead:', worker.isDead());
5601cb0ef41Sopenharmony_ci  });
5611cb0ef41Sopenharmony_ci} else {
5621cb0ef41Sopenharmony_ci  // Workers can share any TCP connection. In this case, it is an HTTP server.
5631cb0ef41Sopenharmony_ci  http.createServer((req, res) => {
5641cb0ef41Sopenharmony_ci    res.writeHead(200);
5651cb0ef41Sopenharmony_ci    res.end(`Current process\n ${process.pid}`);
5661cb0ef41Sopenharmony_ci    process.kill(process.pid);
5671cb0ef41Sopenharmony_ci  }).listen(8000);
5681cb0ef41Sopenharmony_ci}
5691cb0ef41Sopenharmony_ci```
5701cb0ef41Sopenharmony_ci
5711cb0ef41Sopenharmony_ci### `worker.kill([signal])`
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci<!-- YAML
5741cb0ef41Sopenharmony_ciadded: v0.9.12
5751cb0ef41Sopenharmony_ci-->
5761cb0ef41Sopenharmony_ci
5771cb0ef41Sopenharmony_ci* `signal` {string} Name of the kill signal to send to the worker
5781cb0ef41Sopenharmony_ci  process. **Default:** `'SIGTERM'`
5791cb0ef41Sopenharmony_ci
5801cb0ef41Sopenharmony_ciThis function will kill the worker. In the primary worker, it does this by
5811cb0ef41Sopenharmony_cidisconnecting the `worker.process`, and once disconnected, killing with
5821cb0ef41Sopenharmony_ci`signal`. In the worker, it does it by killing the process with `signal`.
5831cb0ef41Sopenharmony_ci
5841cb0ef41Sopenharmony_ciThe `kill()` function kills the worker process without waiting for a graceful
5851cb0ef41Sopenharmony_cidisconnect, it has the same behavior as `worker.process.kill()`.
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ciThis method is aliased as `worker.destroy()` for backwards compatibility.
5881cb0ef41Sopenharmony_ci
5891cb0ef41Sopenharmony_ciIn a worker, `process.kill()` exists, but it is not this function;
5901cb0ef41Sopenharmony_ciit is [`kill()`][].
5911cb0ef41Sopenharmony_ci
5921cb0ef41Sopenharmony_ci### `worker.process`
5931cb0ef41Sopenharmony_ci
5941cb0ef41Sopenharmony_ci<!-- YAML
5951cb0ef41Sopenharmony_ciadded: v0.7.0
5961cb0ef41Sopenharmony_ci-->
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ci* {ChildProcess}
5991cb0ef41Sopenharmony_ci
6001cb0ef41Sopenharmony_ciAll workers are created using [`child_process.fork()`][], the returned object
6011cb0ef41Sopenharmony_cifrom this function is stored as `.process`. In a worker, the global `process`
6021cb0ef41Sopenharmony_ciis stored.
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_ciSee: [Child Process module][].
6051cb0ef41Sopenharmony_ci
6061cb0ef41Sopenharmony_ciWorkers will call `process.exit(0)` if the `'disconnect'` event occurs
6071cb0ef41Sopenharmony_cion `process` and `.exitedAfterDisconnect` is not `true`. This protects against
6081cb0ef41Sopenharmony_ciaccidental disconnection.
6091cb0ef41Sopenharmony_ci
6101cb0ef41Sopenharmony_ci### `worker.send(message[, sendHandle[, options]][, callback])`
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ci<!-- YAML
6131cb0ef41Sopenharmony_ciadded: v0.7.0
6141cb0ef41Sopenharmony_cichanges:
6151cb0ef41Sopenharmony_ci  - version: v4.0.0
6161cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/2620
6171cb0ef41Sopenharmony_ci    description: The `callback` parameter is supported now.
6181cb0ef41Sopenharmony_ci-->
6191cb0ef41Sopenharmony_ci
6201cb0ef41Sopenharmony_ci* `message` {Object}
6211cb0ef41Sopenharmony_ci* `sendHandle` {Handle}
6221cb0ef41Sopenharmony_ci* `options` {Object} The `options` argument, if present, is an object used to
6231cb0ef41Sopenharmony_ci  parameterize the sending of certain types of handles. `options` supports
6241cb0ef41Sopenharmony_ci  the following properties:
6251cb0ef41Sopenharmony_ci  * `keepOpen` {boolean} A value that can be used when passing instances of
6261cb0ef41Sopenharmony_ci    `net.Socket`. When `true`, the socket is kept open in the sending process.
6271cb0ef41Sopenharmony_ci    **Default:** `false`.
6281cb0ef41Sopenharmony_ci* `callback` {Function}
6291cb0ef41Sopenharmony_ci* Returns: {boolean}
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ciSend a message to a worker or primary, optionally with a handle.
6321cb0ef41Sopenharmony_ci
6331cb0ef41Sopenharmony_ciIn the primary, this sends a message to a specific worker. It is identical to
6341cb0ef41Sopenharmony_ci[`ChildProcess.send()`][].
6351cb0ef41Sopenharmony_ci
6361cb0ef41Sopenharmony_ciIn a worker, this sends a message to the primary. It is identical to
6371cb0ef41Sopenharmony_ci`process.send()`.
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ciThis example will echo back all messages from the primary:
6401cb0ef41Sopenharmony_ci
6411cb0ef41Sopenharmony_ci```js
6421cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
6431cb0ef41Sopenharmony_ci  const worker = cluster.fork();
6441cb0ef41Sopenharmony_ci  worker.send('hi there');
6451cb0ef41Sopenharmony_ci
6461cb0ef41Sopenharmony_ci} else if (cluster.isWorker) {
6471cb0ef41Sopenharmony_ci  process.on('message', (msg) => {
6481cb0ef41Sopenharmony_ci    process.send(msg);
6491cb0ef41Sopenharmony_ci  });
6501cb0ef41Sopenharmony_ci}
6511cb0ef41Sopenharmony_ci```
6521cb0ef41Sopenharmony_ci
6531cb0ef41Sopenharmony_ci## Event: `'disconnect'`
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_ci<!-- YAML
6561cb0ef41Sopenharmony_ciadded: v0.7.9
6571cb0ef41Sopenharmony_ci-->
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci* `worker` {cluster.Worker}
6601cb0ef41Sopenharmony_ci
6611cb0ef41Sopenharmony_ciEmitted after the worker IPC channel has disconnected. This can occur when a
6621cb0ef41Sopenharmony_ciworker exits gracefully, is killed, or is disconnected manually (such as with
6631cb0ef41Sopenharmony_ci`worker.disconnect()`).
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ciThere may be a delay between the `'disconnect'` and `'exit'` events. These
6661cb0ef41Sopenharmony_cievents can be used to detect if the process is stuck in a cleanup or if there
6671cb0ef41Sopenharmony_ciare long-living connections.
6681cb0ef41Sopenharmony_ci
6691cb0ef41Sopenharmony_ci```js
6701cb0ef41Sopenharmony_cicluster.on('disconnect', (worker) => {
6711cb0ef41Sopenharmony_ci  console.log(`The worker #${worker.id} has disconnected`);
6721cb0ef41Sopenharmony_ci});
6731cb0ef41Sopenharmony_ci```
6741cb0ef41Sopenharmony_ci
6751cb0ef41Sopenharmony_ci## Event: `'exit'`
6761cb0ef41Sopenharmony_ci
6771cb0ef41Sopenharmony_ci<!-- YAML
6781cb0ef41Sopenharmony_ciadded: v0.7.9
6791cb0ef41Sopenharmony_ci-->
6801cb0ef41Sopenharmony_ci
6811cb0ef41Sopenharmony_ci* `worker` {cluster.Worker}
6821cb0ef41Sopenharmony_ci* `code` {number} The exit code, if it exited normally.
6831cb0ef41Sopenharmony_ci* `signal` {string} The name of the signal (e.g. `'SIGHUP'`) that caused
6841cb0ef41Sopenharmony_ci  the process to be killed.
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ciWhen any of the workers die the cluster module will emit the `'exit'` event.
6871cb0ef41Sopenharmony_ci
6881cb0ef41Sopenharmony_ciThis can be used to restart the worker by calling [`.fork()`][] again.
6891cb0ef41Sopenharmony_ci
6901cb0ef41Sopenharmony_ci```js
6911cb0ef41Sopenharmony_cicluster.on('exit', (worker, code, signal) => {
6921cb0ef41Sopenharmony_ci  console.log('worker %d died (%s). restarting...',
6931cb0ef41Sopenharmony_ci              worker.process.pid, signal || code);
6941cb0ef41Sopenharmony_ci  cluster.fork();
6951cb0ef41Sopenharmony_ci});
6961cb0ef41Sopenharmony_ci```
6971cb0ef41Sopenharmony_ci
6981cb0ef41Sopenharmony_ciSee [`child_process` event: `'exit'`][].
6991cb0ef41Sopenharmony_ci
7001cb0ef41Sopenharmony_ci## Event: `'fork'`
7011cb0ef41Sopenharmony_ci
7021cb0ef41Sopenharmony_ci<!-- YAML
7031cb0ef41Sopenharmony_ciadded: v0.7.0
7041cb0ef41Sopenharmony_ci-->
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ci* `worker` {cluster.Worker}
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_ciWhen a new worker is forked the cluster module will emit a `'fork'` event.
7091cb0ef41Sopenharmony_ciThis can be used to log worker activity, and create a custom timeout.
7101cb0ef41Sopenharmony_ci
7111cb0ef41Sopenharmony_ci```js
7121cb0ef41Sopenharmony_ciconst timeouts = [];
7131cb0ef41Sopenharmony_cifunction errorMsg() {
7141cb0ef41Sopenharmony_ci  console.error('Something must be wrong with the connection ...');
7151cb0ef41Sopenharmony_ci}
7161cb0ef41Sopenharmony_ci
7171cb0ef41Sopenharmony_cicluster.on('fork', (worker) => {
7181cb0ef41Sopenharmony_ci  timeouts[worker.id] = setTimeout(errorMsg, 2000);
7191cb0ef41Sopenharmony_ci});
7201cb0ef41Sopenharmony_cicluster.on('listening', (worker, address) => {
7211cb0ef41Sopenharmony_ci  clearTimeout(timeouts[worker.id]);
7221cb0ef41Sopenharmony_ci});
7231cb0ef41Sopenharmony_cicluster.on('exit', (worker, code, signal) => {
7241cb0ef41Sopenharmony_ci  clearTimeout(timeouts[worker.id]);
7251cb0ef41Sopenharmony_ci  errorMsg();
7261cb0ef41Sopenharmony_ci});
7271cb0ef41Sopenharmony_ci```
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ci## Event: `'listening'`
7301cb0ef41Sopenharmony_ci
7311cb0ef41Sopenharmony_ci<!-- YAML
7321cb0ef41Sopenharmony_ciadded: v0.7.0
7331cb0ef41Sopenharmony_ci-->
7341cb0ef41Sopenharmony_ci
7351cb0ef41Sopenharmony_ci* `worker` {cluster.Worker}
7361cb0ef41Sopenharmony_ci* `address` {Object}
7371cb0ef41Sopenharmony_ci
7381cb0ef41Sopenharmony_ciAfter calling `listen()` from a worker, when the `'listening'` event is emitted
7391cb0ef41Sopenharmony_cion the server, a `'listening'` event will also be emitted on `cluster` in the
7401cb0ef41Sopenharmony_ciprimary.
7411cb0ef41Sopenharmony_ci
7421cb0ef41Sopenharmony_ciThe event handler is executed with two arguments, the `worker` contains the
7431cb0ef41Sopenharmony_ciworker object and the `address` object contains the following connection
7441cb0ef41Sopenharmony_ciproperties: `address`, `port`, and `addressType`. This is very useful if the
7451cb0ef41Sopenharmony_ciworker is listening on more than one address.
7461cb0ef41Sopenharmony_ci
7471cb0ef41Sopenharmony_ci```js
7481cb0ef41Sopenharmony_cicluster.on('listening', (worker, address) => {
7491cb0ef41Sopenharmony_ci  console.log(
7501cb0ef41Sopenharmony_ci    `A worker is now connected to ${address.address}:${address.port}`);
7511cb0ef41Sopenharmony_ci});
7521cb0ef41Sopenharmony_ci```
7531cb0ef41Sopenharmony_ci
7541cb0ef41Sopenharmony_ciThe `addressType` is one of:
7551cb0ef41Sopenharmony_ci
7561cb0ef41Sopenharmony_ci* `4` (TCPv4)
7571cb0ef41Sopenharmony_ci* `6` (TCPv6)
7581cb0ef41Sopenharmony_ci* `-1` (Unix domain socket)
7591cb0ef41Sopenharmony_ci* `'udp4'` or `'udp6'` (UDPv4 or UDPv6)
7601cb0ef41Sopenharmony_ci
7611cb0ef41Sopenharmony_ci## Event: `'message'`
7621cb0ef41Sopenharmony_ci
7631cb0ef41Sopenharmony_ci<!-- YAML
7641cb0ef41Sopenharmony_ciadded: v2.5.0
7651cb0ef41Sopenharmony_cichanges:
7661cb0ef41Sopenharmony_ci  - version: v6.0.0
7671cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5361
7681cb0ef41Sopenharmony_ci    description: The `worker` parameter is passed now; see below for details.
7691cb0ef41Sopenharmony_ci-->
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ci* `worker` {cluster.Worker}
7721cb0ef41Sopenharmony_ci* `message` {Object}
7731cb0ef41Sopenharmony_ci* `handle` {undefined|Object}
7741cb0ef41Sopenharmony_ci
7751cb0ef41Sopenharmony_ciEmitted when the cluster primary receives a message from any worker.
7761cb0ef41Sopenharmony_ci
7771cb0ef41Sopenharmony_ciSee [`child_process` event: `'message'`][].
7781cb0ef41Sopenharmony_ci
7791cb0ef41Sopenharmony_ci## Event: `'online'`
7801cb0ef41Sopenharmony_ci
7811cb0ef41Sopenharmony_ci<!-- YAML
7821cb0ef41Sopenharmony_ciadded: v0.7.0
7831cb0ef41Sopenharmony_ci-->
7841cb0ef41Sopenharmony_ci
7851cb0ef41Sopenharmony_ci* `worker` {cluster.Worker}
7861cb0ef41Sopenharmony_ci
7871cb0ef41Sopenharmony_ciAfter forking a new worker, the worker should respond with an online message.
7881cb0ef41Sopenharmony_ciWhen the primary receives an online message it will emit this event.
7891cb0ef41Sopenharmony_ciThe difference between `'fork'` and `'online'` is that fork is emitted when the
7901cb0ef41Sopenharmony_ciprimary forks a worker, and `'online'` is emitted when the worker is running.
7911cb0ef41Sopenharmony_ci
7921cb0ef41Sopenharmony_ci```js
7931cb0ef41Sopenharmony_cicluster.on('online', (worker) => {
7941cb0ef41Sopenharmony_ci  console.log('Yay, the worker responded after it was forked');
7951cb0ef41Sopenharmony_ci});
7961cb0ef41Sopenharmony_ci```
7971cb0ef41Sopenharmony_ci
7981cb0ef41Sopenharmony_ci## Event: `'setup'`
7991cb0ef41Sopenharmony_ci
8001cb0ef41Sopenharmony_ci<!-- YAML
8011cb0ef41Sopenharmony_ciadded: v0.7.1
8021cb0ef41Sopenharmony_ci-->
8031cb0ef41Sopenharmony_ci
8041cb0ef41Sopenharmony_ci* `settings` {Object}
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ciEmitted every time [`.setupPrimary()`][] is called.
8071cb0ef41Sopenharmony_ci
8081cb0ef41Sopenharmony_ciThe `settings` object is the `cluster.settings` object at the time
8091cb0ef41Sopenharmony_ci[`.setupPrimary()`][] was called and is advisory only, since multiple calls to
8101cb0ef41Sopenharmony_ci[`.setupPrimary()`][] can be made in a single tick.
8111cb0ef41Sopenharmony_ci
8121cb0ef41Sopenharmony_ciIf accuracy is important, use `cluster.settings`.
8131cb0ef41Sopenharmony_ci
8141cb0ef41Sopenharmony_ci## `cluster.disconnect([callback])`
8151cb0ef41Sopenharmony_ci
8161cb0ef41Sopenharmony_ci<!-- YAML
8171cb0ef41Sopenharmony_ciadded: v0.7.7
8181cb0ef41Sopenharmony_ci-->
8191cb0ef41Sopenharmony_ci
8201cb0ef41Sopenharmony_ci* `callback` {Function} Called when all workers are disconnected and handles are
8211cb0ef41Sopenharmony_ci  closed.
8221cb0ef41Sopenharmony_ci
8231cb0ef41Sopenharmony_ciCalls `.disconnect()` on each worker in `cluster.workers`.
8241cb0ef41Sopenharmony_ci
8251cb0ef41Sopenharmony_ciWhen they are disconnected all internal handles will be closed, allowing the
8261cb0ef41Sopenharmony_ciprimary process to die gracefully if no other event is waiting.
8271cb0ef41Sopenharmony_ci
8281cb0ef41Sopenharmony_ciThe method takes an optional callback argument which will be called when
8291cb0ef41Sopenharmony_cifinished.
8301cb0ef41Sopenharmony_ci
8311cb0ef41Sopenharmony_ciThis can only be called from the primary process.
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci## `cluster.fork([env])`
8341cb0ef41Sopenharmony_ci
8351cb0ef41Sopenharmony_ci<!-- YAML
8361cb0ef41Sopenharmony_ciadded: v0.6.0
8371cb0ef41Sopenharmony_ci-->
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ci* `env` {Object} Key/value pairs to add to worker process environment.
8401cb0ef41Sopenharmony_ci* Returns: {cluster.Worker}
8411cb0ef41Sopenharmony_ci
8421cb0ef41Sopenharmony_ciSpawn a new worker process.
8431cb0ef41Sopenharmony_ci
8441cb0ef41Sopenharmony_ciThis can only be called from the primary process.
8451cb0ef41Sopenharmony_ci
8461cb0ef41Sopenharmony_ci## `cluster.isMaster`
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_ci<!-- YAML
8491cb0ef41Sopenharmony_ciadded: v0.8.1
8501cb0ef41Sopenharmony_cideprecated: v16.0.0
8511cb0ef41Sopenharmony_ci-->
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated
8541cb0ef41Sopenharmony_ci
8551cb0ef41Sopenharmony_ciDeprecated alias for [`cluster.isPrimary`][].
8561cb0ef41Sopenharmony_ci
8571cb0ef41Sopenharmony_ci## `cluster.isPrimary`
8581cb0ef41Sopenharmony_ci
8591cb0ef41Sopenharmony_ci<!-- YAML
8601cb0ef41Sopenharmony_ciadded: v16.0.0
8611cb0ef41Sopenharmony_ci-->
8621cb0ef41Sopenharmony_ci
8631cb0ef41Sopenharmony_ci* {boolean}
8641cb0ef41Sopenharmony_ci
8651cb0ef41Sopenharmony_ciTrue if the process is a primary. This is determined
8661cb0ef41Sopenharmony_ciby the `process.env.NODE_UNIQUE_ID`. If `process.env.NODE_UNIQUE_ID` is
8671cb0ef41Sopenharmony_ciundefined, then `isPrimary` is `true`.
8681cb0ef41Sopenharmony_ci
8691cb0ef41Sopenharmony_ci## `cluster.isWorker`
8701cb0ef41Sopenharmony_ci
8711cb0ef41Sopenharmony_ci<!-- YAML
8721cb0ef41Sopenharmony_ciadded: v0.6.0
8731cb0ef41Sopenharmony_ci-->
8741cb0ef41Sopenharmony_ci
8751cb0ef41Sopenharmony_ci* {boolean}
8761cb0ef41Sopenharmony_ci
8771cb0ef41Sopenharmony_ciTrue if the process is not a primary (it is the negation of `cluster.isPrimary`).
8781cb0ef41Sopenharmony_ci
8791cb0ef41Sopenharmony_ci## `cluster.schedulingPolicy`
8801cb0ef41Sopenharmony_ci
8811cb0ef41Sopenharmony_ci<!-- YAML
8821cb0ef41Sopenharmony_ciadded: v0.11.2
8831cb0ef41Sopenharmony_ci-->
8841cb0ef41Sopenharmony_ci
8851cb0ef41Sopenharmony_ciThe scheduling policy, either `cluster.SCHED_RR` for round-robin or
8861cb0ef41Sopenharmony_ci`cluster.SCHED_NONE` to leave it to the operating system. This is a
8871cb0ef41Sopenharmony_ciglobal setting and effectively frozen once either the first worker is spawned,
8881cb0ef41Sopenharmony_cior [`.setupPrimary()`][] is called, whichever comes first.
8891cb0ef41Sopenharmony_ci
8901cb0ef41Sopenharmony_ci`SCHED_RR` is the default on all operating systems except Windows.
8911cb0ef41Sopenharmony_ciWindows will change to `SCHED_RR` once libuv is able to effectively
8921cb0ef41Sopenharmony_cidistribute IOCP handles without incurring a large performance hit.
8931cb0ef41Sopenharmony_ci
8941cb0ef41Sopenharmony_ci`cluster.schedulingPolicy` can also be set through the
8951cb0ef41Sopenharmony_ci`NODE_CLUSTER_SCHED_POLICY` environment variable. Valid
8961cb0ef41Sopenharmony_civalues are `'rr'` and `'none'`.
8971cb0ef41Sopenharmony_ci
8981cb0ef41Sopenharmony_ci## `cluster.settings`
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ci<!-- YAML
9011cb0ef41Sopenharmony_ciadded: v0.7.1
9021cb0ef41Sopenharmony_cichanges:
9031cb0ef41Sopenharmony_ci  - version:
9041cb0ef41Sopenharmony_ci     - v13.2.0
9051cb0ef41Sopenharmony_ci     - v12.16.0
9061cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/30162
9071cb0ef41Sopenharmony_ci    description: The `serialization` option is supported now.
9081cb0ef41Sopenharmony_ci  - version: v9.5.0
9091cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18399
9101cb0ef41Sopenharmony_ci    description: The `cwd` option is supported now.
9111cb0ef41Sopenharmony_ci  - version: v9.4.0
9121cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17412
9131cb0ef41Sopenharmony_ci    description: The `windowsHide` option is supported now.
9141cb0ef41Sopenharmony_ci  - version: v8.2.0
9151cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/14140
9161cb0ef41Sopenharmony_ci    description: The `inspectPort` option is supported now.
9171cb0ef41Sopenharmony_ci  - version: v6.4.0
9181cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/7838
9191cb0ef41Sopenharmony_ci    description: The `stdio` option is supported now.
9201cb0ef41Sopenharmony_ci-->
9211cb0ef41Sopenharmony_ci
9221cb0ef41Sopenharmony_ci* {Object}
9231cb0ef41Sopenharmony_ci  * `execArgv` {string\[]} List of string arguments passed to the Node.js
9241cb0ef41Sopenharmony_ci    executable. **Default:** `process.execArgv`.
9251cb0ef41Sopenharmony_ci  * `exec` {string} File path to worker file. **Default:** `process.argv[1]`.
9261cb0ef41Sopenharmony_ci  * `args` {string\[]} String arguments passed to worker.
9271cb0ef41Sopenharmony_ci    **Default:** `process.argv.slice(2)`.
9281cb0ef41Sopenharmony_ci  * `cwd` {string} Current working directory of the worker process. **Default:**
9291cb0ef41Sopenharmony_ci    `undefined` (inherits from parent process).
9301cb0ef41Sopenharmony_ci  * `serialization` {string} Specify the kind of serialization used for sending
9311cb0ef41Sopenharmony_ci    messages between processes. Possible values are `'json'` and `'advanced'`.
9321cb0ef41Sopenharmony_ci    See [Advanced serialization for `child_process`][] for more details.
9331cb0ef41Sopenharmony_ci    **Default:** `false`.
9341cb0ef41Sopenharmony_ci  * `silent` {boolean} Whether or not to send output to parent's stdio.
9351cb0ef41Sopenharmony_ci    **Default:** `false`.
9361cb0ef41Sopenharmony_ci  * `stdio` {Array} Configures the stdio of forked processes. Because the
9371cb0ef41Sopenharmony_ci    cluster module relies on IPC to function, this configuration must contain an
9381cb0ef41Sopenharmony_ci    `'ipc'` entry. When this option is provided, it overrides `silent`. See
9391cb0ef41Sopenharmony_ci    [`child_process.spawn()`][]'s [`stdio`][].
9401cb0ef41Sopenharmony_ci  * `uid` {number} Sets the user identity of the process. (See setuid(2).)
9411cb0ef41Sopenharmony_ci  * `gid` {number} Sets the group identity of the process. (See setgid(2).)
9421cb0ef41Sopenharmony_ci  * `inspectPort` {number|Function} Sets inspector port of worker.
9431cb0ef41Sopenharmony_ci    This can be a number, or a function that takes no arguments and returns a
9441cb0ef41Sopenharmony_ci    number. By default each worker gets its own port, incremented from the
9451cb0ef41Sopenharmony_ci    primary's `process.debugPort`.
9461cb0ef41Sopenharmony_ci  * `windowsHide` {boolean} Hide the forked processes console window that would
9471cb0ef41Sopenharmony_ci    normally be created on Windows systems. **Default:** `false`.
9481cb0ef41Sopenharmony_ci
9491cb0ef41Sopenharmony_ciAfter calling [`.setupPrimary()`][] (or [`.fork()`][]) this settings object will
9501cb0ef41Sopenharmony_cicontain the settings, including the default values.
9511cb0ef41Sopenharmony_ci
9521cb0ef41Sopenharmony_ciThis object is not intended to be changed or set manually.
9531cb0ef41Sopenharmony_ci
9541cb0ef41Sopenharmony_ci## `cluster.setupMaster([settings])`
9551cb0ef41Sopenharmony_ci
9561cb0ef41Sopenharmony_ci<!-- YAML
9571cb0ef41Sopenharmony_ciadded: v0.7.1
9581cb0ef41Sopenharmony_cideprecated: v16.0.0
9591cb0ef41Sopenharmony_cichanges:
9601cb0ef41Sopenharmony_ci  - version: v6.4.0
9611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/7838
9621cb0ef41Sopenharmony_ci    description: The `stdio` option is supported now.
9631cb0ef41Sopenharmony_ci-->
9641cb0ef41Sopenharmony_ci
9651cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated
9661cb0ef41Sopenharmony_ci
9671cb0ef41Sopenharmony_ciDeprecated alias for [`.setupPrimary()`][].
9681cb0ef41Sopenharmony_ci
9691cb0ef41Sopenharmony_ci## `cluster.setupPrimary([settings])`
9701cb0ef41Sopenharmony_ci
9711cb0ef41Sopenharmony_ci<!-- YAML
9721cb0ef41Sopenharmony_ciadded: v16.0.0
9731cb0ef41Sopenharmony_ci-->
9741cb0ef41Sopenharmony_ci
9751cb0ef41Sopenharmony_ci* `settings` {Object} See [`cluster.settings`][].
9761cb0ef41Sopenharmony_ci
9771cb0ef41Sopenharmony_ci`setupPrimary` is used to change the default 'fork' behavior. Once called,
9781cb0ef41Sopenharmony_cithe settings will be present in `cluster.settings`.
9791cb0ef41Sopenharmony_ci
9801cb0ef41Sopenharmony_ciAny settings changes only affect future calls to [`.fork()`][] and have no
9811cb0ef41Sopenharmony_cieffect on workers that are already running.
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_ciThe only attribute of a worker that cannot be set via `.setupPrimary()` is
9841cb0ef41Sopenharmony_cithe `env` passed to [`.fork()`][].
9851cb0ef41Sopenharmony_ci
9861cb0ef41Sopenharmony_ciThe defaults above apply to the first call only; the defaults for later
9871cb0ef41Sopenharmony_cicalls are the current values at the time of `cluster.setupPrimary()` is called.
9881cb0ef41Sopenharmony_ci
9891cb0ef41Sopenharmony_ci```mjs
9901cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
9911cb0ef41Sopenharmony_ci
9921cb0ef41Sopenharmony_cicluster.setupPrimary({
9931cb0ef41Sopenharmony_ci  exec: 'worker.js',
9941cb0ef41Sopenharmony_ci  args: ['--use', 'https'],
9951cb0ef41Sopenharmony_ci  silent: true,
9961cb0ef41Sopenharmony_ci});
9971cb0ef41Sopenharmony_cicluster.fork(); // https worker
9981cb0ef41Sopenharmony_cicluster.setupPrimary({
9991cb0ef41Sopenharmony_ci  exec: 'worker.js',
10001cb0ef41Sopenharmony_ci  args: ['--use', 'http'],
10011cb0ef41Sopenharmony_ci});
10021cb0ef41Sopenharmony_cicluster.fork(); // http worker
10031cb0ef41Sopenharmony_ci```
10041cb0ef41Sopenharmony_ci
10051cb0ef41Sopenharmony_ci```cjs
10061cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
10071cb0ef41Sopenharmony_ci
10081cb0ef41Sopenharmony_cicluster.setupPrimary({
10091cb0ef41Sopenharmony_ci  exec: 'worker.js',
10101cb0ef41Sopenharmony_ci  args: ['--use', 'https'],
10111cb0ef41Sopenharmony_ci  silent: true,
10121cb0ef41Sopenharmony_ci});
10131cb0ef41Sopenharmony_cicluster.fork(); // https worker
10141cb0ef41Sopenharmony_cicluster.setupPrimary({
10151cb0ef41Sopenharmony_ci  exec: 'worker.js',
10161cb0ef41Sopenharmony_ci  args: ['--use', 'http'],
10171cb0ef41Sopenharmony_ci});
10181cb0ef41Sopenharmony_cicluster.fork(); // http worker
10191cb0ef41Sopenharmony_ci```
10201cb0ef41Sopenharmony_ci
10211cb0ef41Sopenharmony_ciThis can only be called from the primary process.
10221cb0ef41Sopenharmony_ci
10231cb0ef41Sopenharmony_ci## `cluster.worker`
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci<!-- YAML
10261cb0ef41Sopenharmony_ciadded: v0.7.0
10271cb0ef41Sopenharmony_ci-->
10281cb0ef41Sopenharmony_ci
10291cb0ef41Sopenharmony_ci* {Object}
10301cb0ef41Sopenharmony_ci
10311cb0ef41Sopenharmony_ciA reference to the current worker object. Not available in the primary process.
10321cb0ef41Sopenharmony_ci
10331cb0ef41Sopenharmony_ci```mjs
10341cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
10351cb0ef41Sopenharmony_ci
10361cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
10371cb0ef41Sopenharmony_ci  console.log('I am primary');
10381cb0ef41Sopenharmony_ci  cluster.fork();
10391cb0ef41Sopenharmony_ci  cluster.fork();
10401cb0ef41Sopenharmony_ci} else if (cluster.isWorker) {
10411cb0ef41Sopenharmony_ci  console.log(`I am worker #${cluster.worker.id}`);
10421cb0ef41Sopenharmony_ci}
10431cb0ef41Sopenharmony_ci```
10441cb0ef41Sopenharmony_ci
10451cb0ef41Sopenharmony_ci```cjs
10461cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
10471cb0ef41Sopenharmony_ci
10481cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
10491cb0ef41Sopenharmony_ci  console.log('I am primary');
10501cb0ef41Sopenharmony_ci  cluster.fork();
10511cb0ef41Sopenharmony_ci  cluster.fork();
10521cb0ef41Sopenharmony_ci} else if (cluster.isWorker) {
10531cb0ef41Sopenharmony_ci  console.log(`I am worker #${cluster.worker.id}`);
10541cb0ef41Sopenharmony_ci}
10551cb0ef41Sopenharmony_ci```
10561cb0ef41Sopenharmony_ci
10571cb0ef41Sopenharmony_ci## `cluster.workers`
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ci<!-- YAML
10601cb0ef41Sopenharmony_ciadded: v0.7.0
10611cb0ef41Sopenharmony_ci-->
10621cb0ef41Sopenharmony_ci
10631cb0ef41Sopenharmony_ci* {Object}
10641cb0ef41Sopenharmony_ci
10651cb0ef41Sopenharmony_ciA hash that stores the active worker objects, keyed by `id` field. This makes it
10661cb0ef41Sopenharmony_cieasy to loop through all the workers. It is only available in the primary
10671cb0ef41Sopenharmony_ciprocess.
10681cb0ef41Sopenharmony_ci
10691cb0ef41Sopenharmony_ciA worker is removed from `cluster.workers` after the worker has disconnected
10701cb0ef41Sopenharmony_ci_and_ exited. The order between these two events cannot be determined in
10711cb0ef41Sopenharmony_ciadvance. However, it is guaranteed that the removal from the `cluster.workers`
10721cb0ef41Sopenharmony_cilist happens before the last `'disconnect'` or `'exit'` event is emitted.
10731cb0ef41Sopenharmony_ci
10741cb0ef41Sopenharmony_ci```mjs
10751cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
10761cb0ef41Sopenharmony_ci
10771cb0ef41Sopenharmony_cifor (const worker of Object.values(cluster.workers)) {
10781cb0ef41Sopenharmony_ci  worker.send('big announcement to all workers');
10791cb0ef41Sopenharmony_ci}
10801cb0ef41Sopenharmony_ci```
10811cb0ef41Sopenharmony_ci
10821cb0ef41Sopenharmony_ci```cjs
10831cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
10841cb0ef41Sopenharmony_ci
10851cb0ef41Sopenharmony_cifor (const worker of Object.values(cluster.workers)) {
10861cb0ef41Sopenharmony_ci  worker.send('big announcement to all workers');
10871cb0ef41Sopenharmony_ci}
10881cb0ef41Sopenharmony_ci```
10891cb0ef41Sopenharmony_ci
10901cb0ef41Sopenharmony_ci[Advanced serialization for `child_process`]: child_process.md#advanced-serialization
10911cb0ef41Sopenharmony_ci[Child Process module]: child_process.md#child_processforkmodulepath-args-options
10921cb0ef41Sopenharmony_ci[`.fork()`]: #clusterforkenv
10931cb0ef41Sopenharmony_ci[`.setupPrimary()`]: #clustersetupprimarysettings
10941cb0ef41Sopenharmony_ci[`ChildProcess.send()`]: child_process.md#subprocesssendmessage-sendhandle-options-callback
10951cb0ef41Sopenharmony_ci[`child_process.fork()`]: child_process.md#child_processforkmodulepath-args-options
10961cb0ef41Sopenharmony_ci[`child_process.spawn()`]: child_process.md#child_processspawncommand-args-options
10971cb0ef41Sopenharmony_ci[`child_process` event: `'exit'`]: child_process.md#event-exit
10981cb0ef41Sopenharmony_ci[`child_process` event: `'message'`]: child_process.md#event-message
10991cb0ef41Sopenharmony_ci[`cluster.isPrimary`]: #clusterisprimary
11001cb0ef41Sopenharmony_ci[`cluster.settings`]: #clustersettings
11011cb0ef41Sopenharmony_ci[`disconnect()`]: child_process.md#subprocessdisconnect
11021cb0ef41Sopenharmony_ci[`kill()`]: process.md#processkillpid-signal
11031cb0ef41Sopenharmony_ci[`process` event: `'message'`]: process.md#event-message
11041cb0ef41Sopenharmony_ci[`server.close()`]: net.md#event-close
11051cb0ef41Sopenharmony_ci[`stdio`]: child_process.md#optionsstdio
11061cb0ef41Sopenharmony_ci[`worker.exitedAfterDisconnect`]: #workerexitedafterdisconnect
11071cb0ef41Sopenharmony_ci[`worker_threads`]: worker_threads.md
1108