Wrapper for child_process.fork and EventEmitter. Features automatic restarts of the child on exit, close, disconnect and error.


Simple wrapper for child_process.fork & EventEmitter. Features automatic restarts of the child process in case of error, exit, close and disconnect.

  1. Installation

npm install evented-forker

  1. Usage

var Forker = require('evented-forker').Forker;
    Instantiate a new Forker with:
    0: Path to the script you want to execute
    1: Timeout after a exit, close etc (ms)
    2: Setting of { silent: true } in the fork. See http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options.
var forker = new Forker(__dirname + '/sub.js', 1000, false);
forker.on('message', function(payload) {
    // Do something with the payload 
    console.log('Message:', payload);
forker.on('exit', function() {
    // Forked script exited and restarted after timeout 
forker.on('error', function() {
    // Forked script error'd and restarted after timeout 
forker.on('disconnect', function() {
    // Forked script disconnected and restarted after timeout 
forker.on('close', function() {
    // Forked script closed and restarted after timeout 
  1. Alternative syntax

Forker can also be instantiated using this alternative syntax:

var forker = require('evented-forker').create(__dirname + '/sub.js', 1000, false);
  1. License

(c) 2014, Starting Point. MIT License. See LICENSE for the full text.