Package Exports
- cluster-eventdispatcher
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cluster-eventdispatcher) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
cluster-eventdispatcher
Small and simple EventDispatcher, which executes events in all processes.
Install
npm i --save cluster-eventdispatcher
How to use
- Create an instance of Eventdispatcher (whether in the master or in a worker):
const EventDispatcher = require('cluster-eventdispatcher')
/* ... */
const dispatcher = new EventDispatcher()
- Listen to events:
dispatcher.on('eventname', eventdata => {
/* Do what you want*/
})
/* OR */
dispatcher.once('eventname', eventdata => {
/* Do what you want*/
})
- Dispatch Events:
dispatcher.dispatch('eventname', {
/* your data here */
})
Documentation
Documentation is either available online at Github Pages or can be generated using:
npm i
npm run doc
Full Example
File is also available in the example
directory
const EventDispatcher = require('cluster-eventdispatcher')
const cluster = require('cluster')
const express = require('express')
const app = express()
if(cluster.isMaster){
const dispatcher = new EventDispatcher()
for(let i = 0; i < require('os').cpus().length; i++){
const worker = cluster.fork()
dispatcher.initWorker(worker)
}
dispatcher.on('call', data => {
console.log('/ was called: ', data)
dispatcher.dispatch('response', 'hello')
})
} else {
const dispatcher = new EventDispatcher()
dispatcher.on('response', data => console.log(data))
app.get('/', (req, res) => {
res.send('cluster-eventdispatcher example')
dispatcher.dispatch('call', {
someData: 'data'
})
})
app.listen(8080)
}