For whatever reason, in Node.js, the function process.send is defined in some environments but not defined in others. For instance, when I fork a child process from a parent process in Node.js like so:
//parent process
var cp = require('child_process');
var k = cp.fork('./child.js',['arg1','arg2','arg3']);
k.send('fail'); //k.send is defined...
process.send("ok, let's try this..."); //process.send is NOT defined
inside the child process:
//child.js
process.send('message'); //process.send is defined, and will send a message to the parent process above
why is process.send
conditionally defined in some Node.js processes but not others? Seems like a poor design decision by Node.js architects.
The only way I know how to get around this is:
if (typeof process.send === 'function') {
process.send('what I want to send');
}
The process. send() method is an inbuilt application programming interface of the process module which is used by the child process to communicate with the parent process.
send(req, path, [options]) Create a new SendStream for the given path to send to a res . The req is the Node. js HTTP request and the path is a urlencoded path to send (urlencoded, not the actual file-system path).
Node. js runs in a single process and the application code runs in a single thread and thereby needs less resources than other platforms. All the user requests to your web application will be handled by a single thread and all the I/O work or long running job is performed asynchronously for a particular request.
Child processes have a process.send
method to communicate back with the process that spawned them, while the root process doesn't have any "parent" to communicate, so its not there. From the docs:
In the child the process object will have a send() method, and process will emit objects each time it receives a message on its channel.
To avoid having to "litter the code with conditionals", a temporary solution might be to just put a "noop" function in its place at the top of any "root" files you might be spawning processes from:
process.send = process.send || function () {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With