cluster-rpc
A simple way to wrap node.js modules for use with cluster even though they were designed to be used in a single non-cluster instance.
Install
npm install --save cluster-rpc
Usage
In the master process you will create the real instance of whatever module you're trying to use (for example express-session/session/memory, sqlite3, level) and then you will supply the names of the methods you wish to export to worker processes.
By default each worker will be added when cluster
emits a fork
event.
If needed you can set addOnFork
to false
and call addWorker(worker)
manually.
master
// You can pick any module with thunk-style callbacks// For example:var db = './mydb' // Wrap the instancevar crpc = ; // If you set addOnFork to false, You must manually add each worker// crpc.addWorker(cluster.fork()); crpc;
worker
// retrieve the instancevar crpc = ; // all listed methods will be rpc'dcrpc;
Example
'use strict'; var cluster = ;var crpc; if clusterisMaster crpc = ; crpc; crpc; else crpc = ; crpc;