Tuesday, April 7, 2020

Node.js Event Module

Node.js Event Module

The Node.js core API is based upon an asynchronous event-driven architecture and Node.js is perfectly suitable for event-driven applications. We can create and handle custom events using Node.js. Special kinds of objects known as emitters have the ability to emit the events identified by their names and cause special kinds of functions known as listeners to be invoked.

All objects that have the capability to emit events are instances of the EventEmitter class. These objects can fire events, for example, the readStream object fires events when a file is opened and closed. Generally, names of the events are camel-cased strings, however, any legitimate property of JavaScript can be used.

Events Module

Built-in Module “events” can be used to create, fire and listen to custom events. We can use the require() function to include this module.

var events = require('events');
var eventEmitter = new events.EventEmitter();

All of the attached functions to an EventEmitter object are called synchronously upon the emission of an event. If any values are returned by the listener functions these values are ignored and discarded.

To demonstrate simple EventEmitter with a single listener, The eventEmitter.on() method is used to register the listeners. The eventEmitter.emit() method is added to trigger this event. The eventEmitter.on() method requires the name of the event to handle and a callback function which is called when an event is raised.

//include the events module using require function
//create the reference of EventEmitter class
var test_events = require('events');

//create an object of EventEmitter class by using above reference
var emitter = new test_events.EventEmitter();

//Register for the Event
emitter.on('TestEvent', function (data) {
 console.log('Event->: ' + data);

//Raising FirstEvent

emitter.emit('TestEvent', 'This is a Test Event');

Event->: This is a Test Event

EventEmitter Methods

emitter.addListener(event, listener)

This method adds a listener at the end of the listener's array for the provided event. No checks are applied to test if the listener is already added to the array.

emitter.on(event, listener)

This method adds a listener to the end of the listener's array for the provided event. No checks are applied to test if the listener is already been added. This method can also be assumed as an alias of emitter.addListener() method.

emitter.once(event, listener)

This method will add a listener(one-time use) for the event. This listener is called only once, after that it will be detached.

emitter.removeListener(event, listener)

This method detaches a listener from the listener array for the provided event.


This method removes all the listeners attached to the provided event.


This method prints a warning if more than 10 listeners are attached for a particular event.


This method returns the current maximum listeners for the emitter that is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.


This method returns a copy of the array of listeners for the provided event.

emitter.emit(event[, arg1][, arg2][, ...])

This method raises the provided events with the input arguments.


This method returns the number of listeners listening to the type of event.

Creating custom event and handling 

var event = require('events').EventEmitter;

function MyProcessor(num) {
  var ev = new event();
  setTimeout(function () {
      for (var i = 1; i <= num; i++) {
          ev.emit('StartLoop', i);
          console.log('Processing number:' + i);
          ev.emit('EndLoop', i);
  , 2000)
  return ev;
var loop = MyProcessor(4);

loop.on('StartLoop', function (data) {
  console.log('process started for ' + data);

loop.on('EndLoop', function (data) {
  console.log('process Completed for ' + data);


process started for 1
Processing number:1
process Completed for 1
process started for 2
Processing number:2
process Completed for 2
process started for 3
Processing number:3
process Completed for 3
process started for 4
Processing number:4
process Completed for 4