Reputation: 11588
I need to know how can i comunicate from a child process to it's parent. I have tried this:
In my main app:
var spawn = require('child_process').spawn
var cp = spawn('path/to/my/process', params)
cp.on('ready', function(){
console.log('process is ready')
})
In my child process app:
process.emit('ready')
but the console.log('process is ready')
is never executed
Upvotes: 8
Views: 10186
Reputation: 255
Use process.send() method to send messages from child to parent.
// Parent process
const childProcess = require('child_process');
var process = childProcess.fork('child.js');
process.on('message', function (message) {
console.log('Message from Child process : ' + message);
});
And in the child
// child.js
process.send('HELLO from child')
Upvotes: 3
Reputation: 667
Sending messages triggers an "message" event. So you could try:
var cp = require('child_process');
var n = cp.fork('path/to/my/process', params);
n.on('message', function(msg) {
console.log('process is ready');
});
See https://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle_callback
Upvotes: 1