Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: modernize and simplify cluster example #41626

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -1073,29 +1073,17 @@ list happens before the last `'disconnect'` or `'exit'` event is emitted.
```mjs
import cluster from 'cluster';

// Go through all workers
function eachWorker(callback) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
}
eachWorker((worker) => {
for (const worker of Object.values(cluster.workers)) {
worker.send('big announcement to all workers');
});
}
```

```cjs
const cluster = require('cluster');

// Go through all workers
function eachWorker(callback) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
}
eachWorker((worker) => {
for (const worker of Object.values(cluster.workers)) {
worker.send('big announcement to all workers');
});
}
```

Using the worker's unique id is the easiest way to locate the worker.
Expand Down