What the Fork
Subscribe to and publish events between parent and child node processes using the standard node event emitter api or call parent methods directly from the child process and vice versa.
See the example in /example
for full usage.
Getting Started
Install it via npm:
npm install wtfork --save
IPC via Event Emitter
You can subscribe to and publish events between parent and child node processes using the standard node event emitter api.
Instead of:
process;
You can simply do the following in your child process:
// can also be .once()processparent;
Example:
Parent process:
; // create a forked process as normal using standard node fork apiconst myDispatcherProcess = ; // subscribe to the child's `hello` event// can also be .once()myDispatcherProcesschild;
Child process:
// you don't need to require wtfork on the child, it will automatically be required // subscribe the the `helloBackAtYou` event to be received from the parent// can also be .once()processparent; // send a `hello` event to the parent processprocessparent;
IPC via Method Calls
This allows you to setup some methods on the parent and child processes that can be called from either process.
Each method MUST return a promise (but only if you want to receive data back)
The fork
method accepts the same arguments as node's fork but with an optional 4th argument of methods.
The method argument accepts a class instance or an object with methods. Any methods found here
are then available to the child to remotely call via process.parent.methods.someCoolMethod(...args)
.
Note: constructor
methods and private methods (beginning with _) are automatically excluded.
To make child methods available to the parent you need to module export using the standard modules syntax for node. See the child example below.
Optionally you can also call process.parent.setChildMethods(classOrObjectOfMethods)
on the child, this however will replace all methods created via module exports
Example:
Parent process:
;/* Some random class of methods you might want to IPC method call Doesn't have to be a class, can also be an object with methods */ /** * * @param str * @returns */ { return { console; return ; }; } /** * * @param str * @param something * @returns */ { return { console; return ; }; } /** * Some private method - private methods are excluded by default * @param secrets * @private */ { return { console; return ; }; } // create a forked process as normal using standard node fork api// but with an additional 4th param of a class or object containing methodsconst myDispatcherProcess = ; // subscribe to the child's `helloBackAtYou` eventmyDispatcherProcesschild; // send a hello event to the child, the child in turn responds with a helloBackAtYou event.myDispatcherProcesschild;
Child process:
// just export as you would any other module to setup available child methods // or module.exports.simples = function( ...// or export default function simples(...// or export default { ...object with methods }// or export default new MyChildHelperClass();moduleexports = /** * Just resolves with the same value, simples! * @param someVal * @returns */ { return { console; return ; }; } /** * Unnecessary function to quit because we can * @param code */ { console; process; }; // subscribe the the `hello` event to be received from the parentprocessparent; // lets call the parent processes hello method - it should always resolve with no errorprocessparentmethods; // lets call the parent processes goodbye method - it should always errorprocessparentmethods;
Running the parent in this example will produce the following output:
Im a parent method called 'hello' and I just ran: test stringIm a parent method called 'goodbye' and I just ran: test string - wtforkI am the child and I received data for parent event 'hello': {"bar":"foo"}The parent method 'hello' resolved back to the child with: {"some":"data"}I am the parent and I received data for a child event 'helloBackAtYou': {"foo":"bar"}Im a child method called 'simples' and I just ran: meerkatThe child method 'simples' resolved back to the parent with: "meerkat"Child process will now exit as instructed by parent...The parent method 'goodbye' errored back to the child:Error: Please don't leave me! at parent.js:29:21 at new Promise (/Users/Mike/Documents/Personal/Projects/wtfork/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:193:7) at Test.goodbye (parent.js:27:12) at EventEmitter.<anonymous> (index.js:243:89) at emitOne (events.js:90:13) at EventEmitter.emit (events.js:182:7) at ChildProcess.<anonymous> (index.js:199:26) at emitTwo (events.js:100:13) at ChildProcess.emit (events.js:185:7) at handleMessage (internal/child_process.js:718:10) Process finished with exit code 0
A simpler child process could be written as:
; ; // and then on the parent you can just call all the methods available to MyChildHelperClassOfDoom// so you don't really need to use events this way but they're there if you need them. // just 2 lines - simples right? \o/
Notes:
- Each method call is given a unique id when called to allow multiple calls of the same methods simultaneously without event naming conflicts, this uses the
cuid
npm module to produce colission resistant ids. - Each child process is also given it's own unique id using
cuid
. Internally this isn't used that much except for event source verification, however, in terms of tracking child processes without knowing a PID then this is useful - the id can be found atprocess.parent.child_id
on the child and on the parent it can be found atyourForkedProcess.child.id
- Until the child confirms it's ready any child method calls or events sent from the parent will be buffered and sent once ready.
License
MIT