npm install --save ipc-emitter
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. Worker
s) 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.
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.
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.
Configures the Master
to:
- Echo payloads retrieved by its master to its workers.
- Echo payloads retrieved by its workers to its master.
Configures the Master
to stop echoing events.
Configures the Master
to echo events retrieved by its Worker
s, to its own Master
.
When a
Master
is configured to echo events to its ownWorker
s, if itself is not aWorker
a warning is issued.
Configures the Master
to stop echoing events retrieved by its Worker
s, to its own Master
.
Configures the Master
to echo events retrieved by its own Master
, to its Worker
s.
When a
Master
is configured to echo events from its ownMaster
, if itself is not aWorker
a warning is issued. When aMaster
is 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
).
Configures the Master
to stop echoing events retrieved by its own Master
, to its Worker
s.
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.
'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'))
'use strict'
const {worker} = require('ipc-emitter')
console.info('Logger initiated')
worker.on('new-user', (userId) => {
console.info(`log: new user: ${userId}`)
})
'use strict'
const {worker} = require('ipc-emitter')
console.info('Auth initiated')
setTimeout(() => {
worker.emit('new-user', 1)
}, 2000)
Logger initiated
Auth initiated
boot: new user: 1
log: new user: 1