Skip to content

Commit 66f4586

Browse files
committed
cluster: emit worker as first 'message' event arg
It's documented as such but didn't actually behave that way. Bug introduced in commit 66fc8ca ("cluster: emit 'message' event on cluster master"), which is the commit that introduced the event. Fixes: #5126 PR-URL: #5361 Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 0dc216f commit 66f4586

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

doc/api/cluster.markdown

+18
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,29 @@ The `addressType` is one of:
487487

488488
* `worker` {cluster.Worker}
489489
* `message` {Object}
490+
* `handle` {undefined|Object}
490491

491492
Emitted when any worker receives a message.
492493

493494
See [child_process event: 'message'][].
494495

496+
Before Node.js v6.0, this event emitted only the message and the handle,
497+
but not the worker object, contrary to what the documentation stated.
498+
499+
If you need to support older versions and don't need the worker object,
500+
you can work around the discrepancy by checking the number of arguments:
501+
502+
```js
503+
cluster.on('message', function(worker, message, handle) {
504+
if (arguments.length === 2) {
505+
handle = message;
506+
message = worker;
507+
worker = undefined;
508+
}
509+
// ...
510+
});
511+
```
512+
495513
## Event: 'online'
496514

497515
* `worker` {cluster.Worker}

lib/cluster.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ function masterInit() {
341341
process: workerProcess
342342
});
343343

344-
worker.on('message', (message, handle) =>
345-
cluster.emit('message', message, handle)
346-
);
344+
worker.on('message', function(message, handle) {
345+
cluster.emit('message', this, message, handle);
346+
});
347347

348348
worker.process.once('exit', function(exitCode, signalCode) {
349349
/*

test/parallel/test-cluster-message.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ else if (cluster.isMaster) {
8484
worker.on('message', function(message) {
8585
check('master', message === 'message from worker');
8686
});
87-
cluster.on('message', function(message) {
87+
cluster.on('message', function(worker_, message) {
88+
assert.strictEqual(worker_, worker);
8889
check('global', message === 'message from worker');
8990
});
9091

0 commit comments

Comments
 (0)