Skip to content

Instantly share code, notes, and snippets.

@PaulMougel
Created November 19, 2013 08:45
Show Gist options
  • Save PaulMougel/7542247 to your computer and use it in GitHub Desktop.
Save PaulMougel/7542247 to your computer and use it in GitHub Desktop.
Parent-child communication using messages in Node.js
process.on('message', function(m) {
console.log('Just received a message from parent: ' + JSON.stringify(m));
if (m.exit === "true") process.exit();
});
$ node parent.js
Just received a message from parent: {"foo":5}
Just received a message from parent: {"exit":"true"}
$
var cp = require('child_process');
var n = cp.fork('./child.js');
n.send({ foo: 5 });
n.send({ exit: 'true' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment