I have an EventEmitter EE which emits event E. There are multiple event listeners L1, L2 & L3 for the same event E.
Now, I want to periodically check if both event emitter & listeners are functioning properly i.e
- Is EventEmitter EE still emitting event E?
- Are EventListeners L1, L2 & L3 still listening to event E?
- Has the EvenEmitter EE been killed due to some reason or stopped functioning?
- Have the listeners L1, L2 & L3 been killed due to some reason or stopped functioning?
Idea is to have an auto-heal mechanism where system learns that the event emitter & corresponding listeners both have stopped functioning or either of them has stopped functioning & re-registers itself to work without breaking the flow.
How do I go about?
Below is the sample implementation.
var events = require('events');
var eventEmitter = new events.EventEmitter();
//Create an event handler:
var myEventHandler1 = function () {
console.log('I hear a scream 1!');
}
//Create an event handler:
var myEventHandler2 = function () {
console.log('I hear a scream 2!');
}
//Assign the event handler to an event:
eventEmitter.on('scream', myEventHandler1);
//Assign the event handler to an event:
eventEmitter.on('scream', myEventHandler2);
console.log("Listener(s) count: "+eventEmitter.listenersCount('scream');
//Fire the 'scream' event:
eventEmitter.emit('scream');
Output:
Listener(s) count: 2
I hear a scream 1!
I hear a scream 2!
true
PS: I understand event listeners are just a callback.
via Ayaz Pasha
No comments:
Post a Comment