Richeve Bebedor
Richeve Bebedor

Reputation: 1658

Node.js to Node.js communication

How can I use the modules loaded from other Node process from another Node process.

Example I run:

node my_modules

which load MyModule

then I will run another nodejs process:

node grab_modules

which will run GrabModule

GrabModule will attempt to use the functions inside MyModule

Is this possible? And if this is possible how?

Upvotes: 5

Views: 2373

Answers (5)

Uli Köhler
Uli Köhler

Reputation: 13751

If you also require interoperability with other languages and/or high speed, ZeroMQ is also an option. While originally being a plain C library, I have had good experiences with the NodeJS bindings.

There a ZeroMQ binding for almost all popular languages, see http://zeromq.org/bindings:_start

Upvotes: 0

Jan Jůna
Jan Jůna

Reputation: 5074

What about this:

my_modules will work as the program with public api (rest api, xml-rpc, ...) and grab_modules will connect to api and call functions from my_modules

Upvotes: 0

Ivan Fraixedes
Ivan Fraixedes

Reputation: 550

1) To use a module (implementation) not an instance (module loaded somewhere of the process using require) in different process, you only need to require that module wherever you need.

If you run two process, for example, process A that use 'MyModule' and process B that use 'GrabModule', but you only need that 'GrabModule', in process B, can access to the exported properties of 'MyModule' then you only need to use require('path to MyModule').

2) On the other hand, if you need that a process B, can access to a module's state (a module that has been executed, because you use require in somewhere) of a process A, then you need to use a IPC (Inter-process communication) that allows to exchange data between process A and process B, and build or use the same protocol in both, over it.

Depending if your process are in the same machine or in different one, to can use some IPC build in the same OS, as nodejs offer with child fork (http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options) or use an IPC built in some network channel.

For example, you can use the publish/subscribe messaging system of Redis (http://redis.io/topics/pubsub)

Upvotes: 0

Gjorgi Kjosev
Gjorgi Kjosev

Reputation: 1559

What you want is probably dnode:

From the README of dnode:

The server (which hosts the functions to be run):

var dnode = require('dnode');

var server = dnode({
    zing : function (n, cb) { cb(n * 100) }
});
server.listen(5050);

The client (which calls functions on the server and gets their results in a callback)

var dnode = require('dnode');

dnode.connect(5050, function (remote) {
    remote.zing(66, function (n) {
        console.log('n = ' + n);
    });
});

Upvotes: 7

Satoshi Nakajima
Satoshi Nakajima

Reputation: 2113

It depends on what you are trying to do.

If you simply want to reuse the same module (MyModule) from two separate node processes, that's quite easy. You just need to put require('MyModule') in GrabModule and MyModule is accessible when you run grab_module.

If you want to 'share' MyModule including its global variables among two processes, it is much more complex. You need to define a inter-process protocol between two processes (typically REST over socket), and use that protocol to access the module in one process from another process.

Upvotes: 0

Related Questions