Small and simple EventDispatcher, which executes events in all processes.
npm i --save cluster-eventdispatcher
- 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 is either available online at Github Pages or can be generated using:
npm i
npm run doc
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)
}