Node.js Events Module

Introduction to the Node.js events Module

As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture, Node.js relies on the events module. The events module is a built-in module for Node.js and has an EventEmitter object, which emits a named event that causes the execution of the Function objects (“listeners”)

An EventEmitter object has two key functions:

  • The main function of an EventEmitter object is to emit a named event.
  • Another function of an EventEmitter object is to attach or detach one or more event listeners to the named event.

Since the events module is a core module, here’s an illustration for initializing the module.

Firstly, we have to import the events module using the require() function:

const EventEmitter = require(‘events’);

Here, the EventEmitter is a class. Secondly, we have to create a new instance of the EventEmitter class:

const eventEmitter = new events.EventEmitter();

The EventEmitter object is exposed to multiple properties and methods, including on and emit.

  • The on property is used to include a callback function that functions when the event is activated.
  • The emit property is used to activate an event.

Here’s an example is given below to demonstrates the use of these properties:

// Importing the events module
const EventEmitter = require('events');
// Creating a new instance for the EventEmitter class
const emitter = new EventEmitter();
// Creating a callback function for execution
emitter.on('program', () => {
    console.log('This program is running!!');
});
// Activating the event
emitter.emit('program');

The Output of the above program is given as follows:

This program is running!!

Some Methods & Properties of EventEmitter Object

There are numerous methods and properties used in EventEmitter Object. Some of them are listed below:

  • eventNames()
  • addListener()
  • listenerCount()
  • listeners()
  • defaultMaxListener
  • getMaxListener()
  • setMaxListener()
  • prependListener()
  • prependOnceListener()
  • removeListener()
  • removeAllListeners()

eventNames()

The eventNames() method delivers an array that contains all the events for which the listeners have been registered by the emitter.

addListener()

The addListener() method is used to add a specified listener to an event. This method is an alias for on.

listenerCount()

The listenerCount() method returns the number of listener for a specified event 

listeners()

The listeners() method is used to deliver an array containing all the listeners for a specified event.

defaultMaxListener

The defaultMaxListener property is used to set a maximum number of listeners for a specified event. However, only ten listeners can be registered for an event by default.

getMaxListener()

The getMaxListener() method is used to see the maximum number of listeners allowed for an event.

setMaxListener()

The setMaxListener() method is used to modify the maximum number of listeners for a particular event. However, by default, the maximum limit is set to 10.

prependListener()

The prependListener() method is used to add the particular listener as the first event.

prependOnceListener()

The prependOnceListener() method is used to add the particular listener as the first event only for once. However, the listener is removed, when the particular listener has been executed.

removeListener()

The removeListener() method is used to remove the particular listener from an event.

removeAllListener()

The removeAllListener() method is used to remove all the listeners from an event.

Let’s see an example based on the methods described above.

File: events.js

var events = require('events');
var eventEmitter = new events.EventEmitter();
// Initializing the first listener
var myListener = function myListener() {
   console.log('myListener is Initialized.');
}
// Initializing the second listener
var yourListener = function yourListner() {
   console.log('yourListener is Initialized.');
}
// Binding the join event with the myListener function
eventEmitter.addListener('join', myListener);
// Binding the join event with the yourListener function
eventEmitter.on('join', yourListener);
var eventListeners = require('events').EventEmitter.listenerCount
   (eventEmitter,'join');
console.log(eventListeners + "\nThe Listeners are listening to join event");
// Activating the join event 
eventEmitter.emit('join');
// Removing the binding of myListener function
eventEmitter.removeListener('join', myListener);
console.log("myListener is deactivated!!");
// Activating the join event 
eventEmitter.emit('join');
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'join');
console.log(eventListeners + "\nThe Listner is listening to join event!");
console.log("Program has been Ended!");

To execute the above program, type the following command on the Node.js command-line:

$ node events.js

The Output of the same should look, as follows:

2
The Listeners are listening to join event
myListener is Initialized.
yourListener is Initialized.
myListener is deactivated!!
yourListener is Initialized.
1
The Listner is listening to join event!
Program has been Ended!