Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating between two different processes in Node.js

The issue is:

  • Lets assume we have two Node.js processes running: example1.js and example2.js.

  • In example1.js there is function func1(input) which returns result1 as a result.

  • Is there a way from within example2.js to call func1(input) and obtain result1 as the outcome?

From what I've learned about Node.js, I have only found one solution which uses sockets for communication. This is less than ideal however because it would require one process listening on a port. If possible I wish to avoid that.


EDIT: After some questions I'd love to add that in hierarchy example1.js cannot be child process of example2.js, but rather the opposite. Also if it helps -- there can be only one example1.js processing its own data and many example2.js's processing own data + data from first process.

like image 701
Alexey Kamenskiy Avatar asked Apr 18 '12 16:04

Alexey Kamenskiy


People also ask

Is node JS multi process?

Node. js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node. js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests.

What is IPC in Nodejs?

a nodejs module for local and remote Inter Process Communication with full support for Linux, Mac and Windows. It also supports all forms of socket communication from low level unix and windows sockets to UDP and secure TLS and TCP sockets. A great solution for complex multiprocess Neural Networking in Node.JS.

How does Inter Process Communication work?

Interprocess communication is the mechanism provided by the operating system that allows processes to communicate with each other. This communication could involve a process letting another process know that some event has occurred or the transferring of data from one process to another.


2 Answers

The use case you describe makes me think of dnode, with which you can easily expose functions to be called by different processes, coordinated by dnode, which uses network sockets (and socket.io, so you can use the same mechanism in the browser).

Another approach would be to use a message queue, there are many good bindings for different message queues.

The simplest way to my knowledge, is to use child_process.fork():

This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. The channel is written to with child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.

So, for your example, you could have example2.js:

var fork = require('child_process').fork; var example1 = fork(__dirname + '/example1.js');  example1.on('message', function(response) {   console.log(response); });  example1.send({func: 'input'}); 

And example1.js:

function func(input) {   process.send('Hello ' + input); }  process.on('message', function(m) {   func(m); }); 
like image 181
Linus Thiel Avatar answered Oct 01 '22 02:10

Linus Thiel


May be you should try Messenger.js. It can do IPC in a handy way.

You don't have to do the communication between the two processes by yourself.

like image 31
Kaicui Avatar answered Oct 01 '22 03:10

Kaicui