Explain Event Emitter Class in Node.js?

Node is completely event driven. The server consists of one thread processing one event after another. A new request coming in is one kind of event. The server starts processing it and when there is a blocking i/o operation, it does not waits until it completes and instead registers a callback function.

Event driven programming is the core of Node.js. In Node.js the event driven programming is achieved with EventEmitter class. Node.js allows you to create and handle custom events by using events built-in module.

The EventEmitter class is included in the events module. It can be used to raise and handle custom events. A listener will listen to the raised event and have some action performed for the event.to create an EventEmitter object you need to improve the event module. When an eventEmitter instance faces an error, it emits an error event. When a new listener is added, a newListener event is fired. When a listener is removed, a removeListener event is fired.

The eventEmitter provides multiple methods like on() and emit(). The on() method is used to bind a function with the event. The emit() method is used to fire an event.