Created
November 19, 2013 08:45
-
-
Save PaulMougel/7542247 to your computer and use it in GitHub Desktop.
Parent-child communication using messages in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('message', function(m) { | |
console.log('Just received a message from parent: ' + JSON.stringify(m)); | |
if (m.exit === "true") process.exit(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node parent.js | |
Just received a message from parent: {"foo":5} | |
Just received a message from parent: {"exit":"true"} | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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