I've two unrelated node js processes running on the same windows machine. I want these two processes to communicate with each other but without using FileIO (performance issue) and Sockets(security issue). Is there any way we can do that? I've read about Shared Memory and pipe based communication but could not find any implementation for node js.
1 Answers
The answer to this depends on whether one node process is launching the other or not. If one node process is launching the other by using fork()
then it can communicate using process.send()
and process.on('message', ...)
.
// parent.js
const cp = require('child_process');
const n = cp.fork(`${__dirname}/sub.js`);
n.on('message', (m) => {
console.log('PARENT got message:', m);
});
// Causes the child to print: CHILD got message: { hello: 'world' }
n.send({ hello: 'world' });
And for the child process:
// child.js
process.on('message', (m) => {
console.log('CHILD got message:', m);
});
// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }
process.send({ foo: 'bar', baz: NaN });
If on the other hand the processes are launched separately that's a different story.
This first leads to the question what do you mean when you say that you don't want to use sockets for security reasons?
Are you concerned that your node process would be accessible from outside your computer. Because that would only happen if you bind to ip address 0.0.0.0, or one of your public ip addresses. It shouldn't happen if you bind to 127.0.0.1, then it should only be accessible from your computer.
Traffic going to 127.0.0.1 also bypasses the network so it never leaves your computer or goes to your network card.
Also you can use local named sockets which are even less accessible.
If your concern that some other local service would be able to communicate to your channel, you can get around that by passing secret information or encrypting the content.
You can find more details on using named sockets and pipes in the following Stack Overflow answer: How to create a named pipe in node.js?

- 13,051
- 4
- 61
- 89

- 27,650
- 10
- 79
- 80
-
Sorry for this late reply. Thanks @luis for such clear information on security related Socket. I did not know that. We've verified that info and are going with "Named Pipe" solution. Apart from this you first solution is for the child process, I'd clearly mentioned in the title that the processes are UNRELATED, so we can't use stdin and stdout for these processes. – Prasoon Birla May 03 '20 at 15:52
-
Moreover, IPC through shared memory is different to messaging IPC. Shared memory, like the name implies, allows processes to share memory without copying or serializing/deserializing it. It's useful in cases like sharing video buffers for instance, where the bandwidth of copying the memory to many client processes would be a significant processing as well as memory use overhead. – Ben Barkay Nov 18 '22 at 08:08