Skip to content

Commit 66fc8ca

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. Reviewed-by: Sam Roberts <[email protected]> Reviewed-by: Christian Tellnes <[email protected]> Reviewed-by: Stephen Belanger <[email protected]> PR-URL: nodejs#861
1 parent e11fc67 commit 66fc8ca

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}
@@ -530,6 +540,8 @@ created. It is disconnected after the `disconnect` event is emitted.
530540

531541
* `message` {Object}
532542

543+
Similar to the `cluster.on('message')` event, but specific to this worker.
544+
533545
This event is the same as the one provided by `child_process.fork()`.
534546

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

lib/cluster.js

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

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

test/parallel/test-cluster-message.js

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

77
function forEach(obj, fn) {
88
Object.keys(obj).forEach(function(name, index) {
9-
fn(obj[name], name, index);
9+
fn(obj[name], name);
1010
});
1111
}
1212

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

4747
var checks = {
48+
global: {
49+
'receive': false,
50+
'correct': false
51+
},
4852
master: {
4953
'receive': false,
5054
'correct': false
@@ -76,12 +80,15 @@ else if (cluster.isMaster) {
7680
// Spawn worker
7781
var worker = cluster.fork();
7882

79-
// When a IPC message is received form the worker
83+
// When a IPC message is received from the worker
8084
worker.on('message', function(message) {
8185
check('master', message === 'message from worker');
8286
});
87+
cluster.on('message', function(message) {
88+
check('global', message === 'message from worker');
89+
});
8390

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

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

0 commit comments

Comments
 (0)