Understanding the event emitter in the Nodejs

By Haribabu | Posted on January 20, 2020

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?

Nodejs event emitter

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:

  1. Return EventEmitter from a function
  2. 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

Nodejs event emitter

The following table lists all the most important methods of EventEmitter class.

S.noMethodDescription
1emitter.addListener(event, listener)
  • Adds a listener at listeners array end for the defined event. 
  • No checks are made to verify if the listener has already been added to the list.
2emitter.on(event, listener)
  • Adds a listener at listeners array end for the defined event. 
  • This method is to listen to data on a predefined event.
  • It can also can be alternative for emitter.addListener().
3emitter.once(event, listener)
  • It listens to data on a registered event only once.
  • Invoked only the next time the event is triggered, after which it is removed.
4emitter.removeListener(event, listener)
  • Removes a listener from the array for the defined event.
  • If any single listener has been added multiple times,  then removeListener must be called multiple times to remove each instance.
5emitter.removeAllListeners([event])
  • Removes all those listeners of the specified event.
6emitter.setMaxListeners(n)
  • By default, EventEmitters will print a caution if more listeners are added for a particular event.
7emitter.listeners(event)
  • Returns an array copy of listeners for the defined event.
8emitter.emit(event, [arg1], [arg2], […])
  • Raise the specified events with the supplied arguments.
9emitter.getMaxListeners()
  • Returns the maximum listener value for the emitter which is either set by emitter or by itself.
10emitter.listenerCount(type)
  • Returns the number of listeners listening to the type of event.

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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *