Event Emitter is a module that enables proper transmission between objects in Nodejs. Most of the modules built on Nodejs use this Event emitter.
Nodejs core API is based on the asynchronous event-driven architecture in that a specific kind of object called Event emitter. Secondly which emits events periodically that causes listener objects to be called. Such a process of emitting an event is the Event emitter in Nodejs.
What is the Event emitter class in Nodejs?
It is easy to create manage custom events easily by using the event module in Nodejs. In general, an Event module(i.e) “Event Emitter” class primarily used to create and handle custom events in Nodejs.
// get the reference of EventEmitter class of events module var events = require('events'); //create an object of EventEmitter class by using above reference var em = new events.EventEmitter(); //Subscribe for newEvent em.on('newEvent', function (data) { console.log('new subscriber: ' + data); }); // Raising newEvent em.emit('newEvent', 'This is my new Nodejs event emitter example.');
The following steps help to raise an event:
- Import the ‘events’ module
- Create an object of EventEmitter class
- Specify event handler function using on() function
All objects that emit events are considered to be the elements or members of the Event Emitter class. Therefore, these objects exhibit an EventEmitter.on() function that allows functions to be attached to assigned events emitted by the object.
Here we tried to illustrate how the Event emitter class in the event module works Nodejs in the following code:
When an EventEmitter faces any error in a run, it emits an ‘error’ event. Then when a new listener is added, ‘newListener’ event is triggered. And when a listener is removed, ‘removeListener’ event is fired.
Read More: How does the Nodejs event loop work exactly?
EventEmitter provides multiple properties like on and emit.on. Then the property is to hold a function with the event and emit is used to start an event.
- Emit is used to start/fire an event.
- On is used in the execution process that adds a specific callback function when the event is fired.
There are two common patterns to raise an event using the Event Emitter class in Nodejs:
- Return EventEmitter from a function
- Extend the EventEmitter class
In Nodejs, when the EventEmitter object emits an event, all the functions with that specific event go synchronously.
Read More: How To Install Node Version Manager NVM?
Event emitter class methods in Nodejs
The following table lists all the most important methods of EventEmitter class.
S.no | Method | Description |
1 | emitter.addListener(event, listener) |
|
2 | emitter.on(event, listener) |
|
3 | emitter.once(event, listener) |
|
4 | emitter.removeListener(event, listener) |
|
5 | emitter.removeAllListeners([event]) |
|
6 | emitter.setMaxListeners(n) |
|
7 | emitter.listeners(event) |
|
8 | emitter.emit(event, [arg1], [arg2], […]) |
|
9 | emitter.getMaxListeners() |
|
10 | emitter.listenerCount(type) |
|
Final words
Hence, you can use the Event Emitter class to trigger and manage the custom events in Nodejs by implementing the above methods.
Contact us to know more about Nodejs and its developmental advantages. Book your free consultation call now.
Leave a Reply