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:

The Output of the above program is given as follows:

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

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

The Output of the same should look, as follows:

Pin It on Pinterest

Share This