Skip to content

tabone/ipc-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status JavaScript Style Guide npm version

ipc-emitter

Installation

npm install --save ipc-emitter

Master

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:

  1. 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.
  2. 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:

  1. Will stop listening for any events the newly forgotten process might emit.
  2. 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 Master is configured to echo events to its own Workers, if itself is not a Worker a 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 Master is configured to echo events from its own Master, if itself is not a Worker a warning is issued. When a Master is configured to echo events from its own Master, when it recieves an event from the Master, its listeners won't be triggered (this is the work of the Worker).

.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

About

EventEmitter for IPC connected node apps.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published