Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple message passing between cluster master and workers in Node.js
var cluster = require('cluster');
if (cluster.isWorker) {
console.log('Worker ' + process.pid + ' has started.');
// Send message to master process.
process.send({msgFromWorker: 'This is from worker ' + process.pid + '.'})
// Receive messages from the master process.
process.on('message', function(msg) {
console.log('Worker ' + process.pid + ' received message from master.', msg);
});
}
if (cluster.isMaster) {
console.log('Master ' + process.pid + ' has started.');
// Fork workers.
for (var i = 0; i < 2; i++) {
var worker = cluster.fork();
// Receive messages from this worker and handle them in the master process.
worker.on('message', function(msg) {
console.log('Master ' + process.pid + ' received message from worker ' + this.pid + '.', msg);
});
// Send a message from the master process to the worker.
worker.send({msgFromMaster: 'This is from master ' + process.pid + ' to worker ' + worker.pid + '.'});
}
// Be notified when worker processes die.
cluster.on('death', function(worker) {
console.log('Worker ' + worker.pid + ' died.');
});
}
@adrianseeley

This comment has been minimized.

Show comment Hide comment
@adrianseeley

adrianseeley Jun 6, 2013

thanks! great ref.

thanks! great ref.

@jnovack

This comment has been minimized.

Show comment Hide comment
@jnovack

jnovack Aug 7, 2013

This won't work. var inside for?

worker will hold the last worker forked, not each one (specially inside the event callback).

jnovack commented Aug 7, 2013

This won't work. var inside for?

worker will hold the last worker forked, not each one (specially inside the event callback).

@dazld

This comment has been minimized.

Show comment Hide comment
@dazld

dazld Feb 5, 2014

should work fine, as the variable is only being used inside that particular block. if it was leaking outside the block, then yes, it would be a problem (and should be in a closure, or have some kind of binding). nice example, thanks!

dazld commented Feb 5, 2014

should work fine, as the variable is only being used inside that particular block. if it was leaking outside the block, then yes, it would be a problem (and should be in a closure, or have some kind of binding). nice example, thanks!

@ynkm169

This comment has been minimized.

Show comment Hide comment
@ynkm169

ynkm169 Aug 25, 2014

how do u pass message between workers.....pass message to master makes master a bottleneck..............

ynkm169 commented Aug 25, 2014

how do u pass message between workers.....pass message to master makes master a bottleneck..............

@CubixSystem

This comment has been minimized.

Show comment Hide comment
@CubixSystem

CubixSystem Mar 7, 2015

Good example.
Need to replace this.pid for this.process.pid on newer Node versions.
Same for worker.pid

Good example.
Need to replace this.pid for this.process.pid on newer Node versions.
Same for worker.pid

@chbdetta

This comment has been minimized.

Show comment Hide comment
@chbdetta

chbdetta Aug 2, 2015

Great example. Thanks a lot!

chbdetta commented Aug 2, 2015

Great example. Thanks a lot!

@danieldram

This comment has been minimized.

Show comment Hide comment
@danieldram

danieldram Jun 10, 2016

can use os.cpus().length to get number of running workers if you do not know the exact number as indicated in the for loop.
(Be sure to var os = require('os') when using)

danieldram commented Jun 10, 2016

can use os.cpus().length to get number of running workers if you do not know the exact number as indicated in the for loop.
(Be sure to var os = require('os') when using)

@saeidalidadi

This comment has been minimized.

Show comment Hide comment
@saeidalidadi

saeidalidadi Feb 6, 2017

I have a Map() object which I use to store on fly values for my app so how I can share this object between all workers ?
I am using .set() and .get() methods of this object also.

saeidalidadi commented Feb 6, 2017

I have a Map() object which I use to store on fly values for my app so how I can share this object between all workers ?
I am using .set() and .get() methods of this object also.

@Rob--

This comment has been minimized.

Show comment Hide comment
@Rob--

Rob-- Jun 4, 2017

I've created a module to make communicating between the master and worker easier: https://github.com/Rob--/cluster-messages

Rob-- commented Jun 4, 2017

I've created a module to make communicating between the master and worker easier: https://github.com/Rob--/cluster-messages

@vikene

This comment has been minimized.

Show comment Hide comment
@vikene

vikene Jun 8, 2017

Great! thanks 👍

vikene commented Jun 8, 2017

Great! thanks 👍

@BriantAnthony

This comment has been minimized.

Show comment Hide comment
@BriantAnthony

BriantAnthony Jan 1, 2018

Why would workers need to communicate between other workers? Shouldn't the database be the single point of truth that each worker gets any persisted data from?

Why would workers need to communicate between other workers? Shouldn't the database be the single point of truth that each worker gets any persisted data from?

@murilobaixou

This comment has been minimized.

Show comment Hide comment
@murilobaixou

murilobaixou Apr 17, 2018

@BriantAnthony thanks for the reminder (really)!

@BriantAnthony thanks for the reminder (really)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment