Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Inter process communication?

Tags:

node.js

ipc

Does Node.js provide any standard way of doing IPC as it happens in many other languages? I am new to Node.js and all information I found was about using child_process.fork() or sockets.

like image 326
user3352528 Avatar asked Feb 25 '14 18:02

user3352528


1 Answers

Have you tried this node package?

Following their doc, possible example could be for server:

var ipc=require('node-ipc');

ipc.config.id   = 'world';
ipc.config.retry= 1500;

ipc.serve(
    function(){
        ipc.server.on(
            'message',
            function(data,socket){
                ipc.log('got a message : '.debug, data);
                ipc.server.emit(
                    socket,
                    'message',  //this can be anything you want so long as
                                //your client knows.
                    data+' world!'
                );
            }
        );
    }
);

ipc.server.start();

possible solution for client:

 var ipc=require('node-ipc');

ipc.config.id   = 'hello';
ipc.config.retry= 1500;

ipc.connectTo(
    'world',
    function(){
        ipc.of.world.on(
            'connect',
            function(){
                ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
                ipc.of.world.emit(
                    'message',  //any event or message type your server listens for
                    'hello'
                )
            }
        );
        ipc.of.world.on(
            'disconnect',
            function(){
                ipc.log('disconnected from world'.notice);
            }
        );
        ipc.of.world.on(
            'message',  //any event or message type your server listens for
            function(data){
                ipc.log('got a message from world : '.debug, data);
            }
        );
    }
);
like image 89
alexey Avatar answered Oct 06 '22 11:10

alexey