Event emitters are implementations of the publish-subscribe architecture, a messaging pattern where emitters (or publishers) emit event (that are usually key value pairs) to a subscriber.
The event emitter can have many subscriber listening to same event, but each subscriber may be performing a different task based on it’s unique context. This what makes event emitters so powerful. They allow developers to write code that is loosely coupled with minimal dependencies.
So how do they work?
Well, based on the example above each instance of the
EventEmitter class is going in initialize a key value map,
EventEmitter class has two methods
on method registers the subscriber by an event name (which is usually a string) and an event handler. The subscriber’s event name/handler pair are added as key value pairs to the handler store,
emit method reads from the handler store and if an event is found, it applies the
message arguments to the handler methods.
The above example, while functional, is lacking in good features (also it’s poorly implemented since it only allow for one handler per event). It mainly serves as a jumping off point for hacking your own custom event emitter.
Good API Features 
- “*”, for listening on any event
- inheritance, for bubbling events
- queueing, for processing large numbers of events concurrently
- caching, for optimizing performance
Implement an event emitter that can execute multiple handlers, so that the following code runs as expected.