Skip to content

Commit b1606d8

Browse files
committed
cluster: emit 'message' event on cluster master
For consistency with the worker 'exit', 'online', 'disconnect', and 'listening' events which are emitted on worker and cluster, also emit 'message' on cluster.
1 parent 77f3586 commit b1606d8

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/api/cluster.markdown

+12
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ This can be used to restart the worker by calling `.fork()` again.
248248

249249
See [child_process event: 'exit'](child_process.html#child_process_event_exit).
250250

251+
## Event: 'message'
252+
253+
* `worker` {Worker object}
254+
* `message` {Object}
255+
256+
Emitted when any worker receives a message.
257+
258+
See
259+
[child_process event: 'message'](child_process.html#child_process_event_message).
260+
251261
## Event: 'setup'
252262

253263
* `settings` {Object}
@@ -528,6 +538,8 @@ created. It is disconnected after the `disconnect` event is emitted.
528538

529539
* `message` {Object}
530540

541+
Similar to the `cluster.on('message')` event, but specific to this worker.
542+
531543
This event is the same as the one provided by `child_process.fork()`.
532544

533545
In a worker you can also use `process.on('message')`.

lib/cluster.js

+2
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ function masterInit() {
318318
process: workerProcess
319319
});
320320

321+
worker.on('message', this.emit.bind(this, 'message'));
322+
321323
function removeWorker(worker) {
322324
assert(worker);
323325

test/parallel/test-cluster-message.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var net = require('net');
55

66
function forEach(obj, fn) {
77
Object.keys(obj).forEach(function(name, index) {
8-
fn(obj[name], name, index);
8+
fn(obj[name], name);
99
});
1010
}
1111

@@ -44,6 +44,10 @@ if (cluster.isWorker) {
4444
else if (cluster.isMaster) {
4545

4646
var checks = {
47+
global: {
48+
'receive': false,
49+
'correct': false
50+
},
4751
master: {
4852
'receive': false,
4953
'correct': false
@@ -75,12 +79,15 @@ else if (cluster.isMaster) {
7579
// Spawn worker
7680
var worker = cluster.fork();
7781

78-
// When a IPC message is received form the worker
82+
// When a IPC message is received from the worker
7983
worker.on('message', function(message) {
8084
check('master', message === 'message from worker');
8185
});
86+
cluster.on('message', function(message) {
87+
check('global', message === 'message from worker');
88+
});
8289

83-
// When a TCP connection is made with the worker connect to it
90+
// When a TCP server is listening in the worker connect to it
8491
worker.on('listening', function() {
8592

8693
client = net.connect(common.PORT, function() {

0 commit comments

Comments
 (0)