ipc-emitter v0.2.1
ipc-emitter
Installation
npm install --save ipc-emitterMaster
const {master} = require('ipc-emitter')Master is an EventEmitter, with a few differences.
When it emits an event (using the .emit() function) apart from triggering its own listeners, it also notifies other acknowledged processes (i.e. Workers) through the IPC Channel (using process.send() method).
In addition to this, it also listens for events emitted by acknowledged processes (by listening for their message event) so that it triggers its own listeners and also notifies other acknowledged processes to trigger their own (note that the process which triggered the event is not notified).
When getting a Master IPC-Emitter it will always return a new object.
API
.ack( process [, process... ] )
Acknowledges a process (Worker). Doing so the Master:
- Will be listening for any events the newly acknowleged process might emit so that it can trigger its own listeners and notify other acknowledged processes.
- Will notify the newly acknowledged process of any events emitted either the master or other acknowledged processes.
.forget( process [, process... ] )
Removes a process (Worker) from the list of acknowledged processes. Doing so the Master:
- Will stop listening for any events the newly forgotten process might emit.
- Will stop notifing the newly forgotten process of any events emitted either by the master or other acknowledged processes.
.echo()
Configures the Master to:
1. Echo payloads retrieved by its master to its workers.
2. Echo payloads retrieved by its workers to its master.
.stopEcho()
Configures the Master to stop echoing events.
.echoUp()
Configures the Master to echo events retrieved by its Workers, to its own Master.
When a
Masteris configured to echo events to its ownWorkers, if itself is not aWorkera warning is issued.
.stopEchoUp()
Configures the Master to stop echoing events retrieved by its Workers, to its own Master.
.echoDown()
Configures the Master to echo events retrieved by its own Master, to its Workers.
When a
Masteris configured to echo events from its ownMaster, if itself is not aWorkera warning is issued. When aMasteris configured to echo events from its ownMaster, when it recieves an event from theMaster, its listeners won't be triggered (this is the work of theWorker).
.stopEchoDown()
Configures the Master to stop echoing events retrieved by its own Master, to its Workers.
Worker
const {worker} = require('ipc-emitter')Worker is an EventEmitter, with a few differences.
When it emits an event (using the .emit() function) apart from triggering its own listeners, it also notifies its master process through the IPC Channel (using process.send() method). Doing this if the Master Process is using the Master IPC-Emitter, the event will be echoed to all of the acknowledged workers.
When getting a Worker IPC-Emitter will always return the same object.
Example
boot.js
'use strict'
const {fork} = require('child_process')
const {master} = require('ipc-emitter')
master.on('new-user', (userId) => {
console.info(`boot: new user: ${userId}`)
})
master.ack(fork('./auth'), fork('./log'))log.js
'use strict'
const {worker} = require('ipc-emitter')
console.info('Logger initiated')
worker.on('new-user', (userId) => {
console.info(`log: new user: ${userId}`)
})auth.js
'use strict'
const {worker} = require('ipc-emitter')
console.info('Auth initiated')
setTimeout(() => {
worker.emit('new-user', 1)
}, 2000)Output
Logger initiated
Auth initiated
boot: new user: 1
log: new user: 1