Skip to content

Commit

Permalink
cluster: emit 'message' event on cluster master
Browse files Browse the repository at this point in the history
For consistency with the worker 'exit', 'online', 'disconnect', and
'listening' events which are emitted on worker and cluster, also emit
'message' on cluster.
  • Loading branch information
sam-github committed Feb 17, 2015
1 parent 77f3586 commit b1606d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
12 changes: 12 additions & 0 deletions doc/api/cluster.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ This can be used to restart the worker by calling `.fork()` again.

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

## Event: 'message'

* `worker` {Worker object}
* `message` {Object}

Emitted when any worker receives a message.

See
[child_process event: 'message'](child_process.html#child_process_event_message).

## Event: 'setup'

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

* `message` {Object}

Similar to the `cluster.on('message')` event, but specific to this worker.

This event is the same as the one provided by `child_process.fork()`.

In a worker you can also use `process.on('message')`.
Expand Down
2 changes: 2 additions & 0 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ function masterInit() {
process: workerProcess
});

worker.on('message', this.emit.bind(this, 'message'));

function removeWorker(worker) {
assert(worker);

Expand Down
13 changes: 10 additions & 3 deletions test/parallel/test-cluster-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var net = require('net');

function forEach(obj, fn) {
Object.keys(obj).forEach(function(name, index) {
fn(obj[name], name, index);
fn(obj[name], name);
});
}

Expand Down Expand Up @@ -44,6 +44,10 @@ if (cluster.isWorker) {
else if (cluster.isMaster) {

var checks = {
global: {
'receive': false,
'correct': false
},
master: {
'receive': false,
'correct': false
Expand Down Expand Up @@ -75,12 +79,15 @@ else if (cluster.isMaster) {
// Spawn worker
var worker = cluster.fork();

// When a IPC message is received form the worker
// When a IPC message is received from the worker
worker.on('message', function(message) {
check('master', message === 'message from worker');
});
cluster.on('message', function(message) {
check('global', message === 'message from worker');
});

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

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

0 comments on commit b1606d8

Please sign in to comment.