You can set an event handler for process.on('message')
to receive messages from the master
in the child_process
script, and receive messages from the child_process
instance by listening to the child.on('message')
event.
Example:
child.js
process.on('message', function(message) {
process.send('Hey you sent ' + m);
});
master.js
var fork = require('child_process').fork;
var worker = fork('./worker.js');
child.on('message', function(m) {
console.log('Recieved: ' + m);
});
// we could send as many messages as we want, the child process will
// will idle between messages
child.send('Message 1');
// Whenever you're done you can kill the child process
child.kill() // Default signal is `SIGTERM`