Interprocess Communication


Greppy is able to easily build cluster applications with multiple contexts and bundled modules. Any context should da exactly one job, e.g. serve a website, a administration frontend, a REST service or something similar. There are some usecases where you need to communicate between these contextes on the same physical machine. This is called Interprocess Communication, or IPC.

Greppy gives you the posibility to define IPC API's. This could let a worker of a administration context talk to the master of this context. So you could build a special mechanism to lock a system resource, log an interaction a user do or collect data for a statistic. There a plenty of other usecases you could build with the help of IPC.

Master

Broadcast Listening

setTimeout(function() {

    master.getIPC().broadcast('gracefull.shutdown', {
        shutdownTime: new Date()
    });

}, 2000);

Interval based IPC request/response

master.getIPC().addMethod('notify.request', function(msg, options, callback) {

    callback && callback(null, {
        test: ++options.test
    });
});

Worker

Broadcast Listening

worker.getIPC().addBroadcastListener('gracefull.shutdown', function(err, result, msg) {
    console.log(err, result, msg);
});

Interval based IPC request/response

var i = 0;

setInterval(function() {

    worker.getIPC().request('notify.request', {
        test: i
    }, function(err, result, msg) {

        i = result.test;
        console.log('cb-worker-side', msg);
    });

}, 1000);