What are event emitter in node.js??

Event Emitter is a module that enables proper transmission between objects in Node.js. Most of the modules built on Node.js uses these EventEmitter. 

Node.js core API is based on the asynchronous event-driven architecture in that a specific kind of object called emitters and which emits events periodically that causes listener object to be called. Such a process of emitting an event is called EventEmitter.

What is Event emitter class??

It is easy to create manage custom events easily by using the event module in Node.js. In general, an Event module(i.e) “EventEmitter” class primarily used to create and handle custom events.

// 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 Node.js event emitter example.');

In the above example, the following steps are to be implemented 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 EventEmitter class. 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 Eventemitter class in the event module works in the following code:

When an EventEmitter faces any error in a run, it emits an ‘error’ event. When a new listener is added, ‘newListener’ event is triggered and when a listener is removed, ‘removeListener’ event is fired.

EventEmitter provides multiple properties like on and emit. on property is used 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 that can be used to raise an event using EventEmitter class in Node.js:

  1. Return EventEmitter from a function
  2. Extend the EventEmitter class

When the EventEmitter object emits an event, all the functions attached to that specific event are called synchronously. All values returned by the called listeners objects are ignored immediately.

Event emitter class methods

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

S.no Method Description
1 emitter.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.
  • Returns emitter, so calls can be chained.
2 emitter.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 be called as an alternative for emitter.addListener().
3 emitter.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.
4 emitter.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.
5 emitter.removeAllListeners([event])
  • Removes all those listeners of the specified event.
6 emitter.setMaxListeners(n)
  • By default, EventEmitters will print a caution if more listeners are added for a particular event.
7 emitter.listeners(event)
  • Returns an array copy of listeners for the defined event.
8 emitter.emit(event, [arg1], [arg2], […])
  • Raise the specified events with the supplied arguments.
9 emitter.getMaxListeners()
  • Returns the maximum listener value for the emitter which is either set by emitter or by itself.
10 emitter.listenerCount(type)
  • Returns the number of listeners listening to the type of event.

Final words

Hence, you can use EventEmitter class to trigger and manage the custom events in Node.js by implementing the above methods.

Leave a Reply

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

Facebook
Twitter
INSTAGRAM
LinkedIn