The Node.js Event emitter

Ranjul Arumadi
Last Updated: May 13, 2022

Introduction

A lot of interactions coming from the user are handled by events. Interactions can be clicking the button, pressing buttons on the keyboard, etc. There has to be a way to service these events. Luckily Node.js has a module called events. This module offers classes using which we can handle our events. In this blog, we are going to talk about the Node.js event emitter.

Initialising the event emitter

To initialize the Node.js event emitter, we first have to include the 'events' module. 

 

// Import events module
const NodeJSEventEmitter = require('events');

// Create an eventEmitter object
const JSEventEmitter = new NodeJSEventEmitter ();

 

This object has many methods. On and emit are two important methods.

  • emit - This method is used to trigger an event
  • on - This method is used to add a callback function that will be executed when the event is triggered.

 

Now let's try creating a start event and react to that by logging into the console:

 

JSEventEmitter.on('start', () => {
 console.log('Start event started');
});

 

The event handler function is triggered when we run the following code and get the console log output.

 

JSEventEmitter.emit('start');

 

Output

Passing arguments to the event handler

We can pass additional arguments to the event handler. We can do this by passing them as additional arguments to the emit function.

 

Here is the code for that:

 

JSEventEmitter.on('start', number => {
 console.log(`Start event started ${number}`);
});

// Passing another parameter 777 here
JSEventEmitter.emit('start',777);

 

 

Output

Passing multiple arguments

We can also pass multiple arguments to the event handler.

 

JSEventEmitter.on('start', (start, end) => {
 console.log(`started from ${start} to ${end}`);
})

// Passing multiple parameters 777 and 888
JSEventEmitter.emit('start',777,888);

 

Output

 

The EventEmitter object also exposes several other methods to interact with events, like

 

Sr. No.MethodDescription
1addListener(event, listener)This adds a listener at the end of the listener's array for the specified event. There is no checking done to see if the listener was already added or not.
2on(event, listener)This adds a listener at the end of the listener's array for the specified event. There is no checking done to see if the listener was already added or not.
3once(event, listener)This adds a one-time listener to the event. The listener is invoked only the next time the event is fired, after which it is removed. 
4emit(event, [arg1], [arg2], [...])Emit executes each of the listeners in order with the supplied arguments. If the event had listeners, then it returns true otherwise, false is returned.
5listeners(event)This returns an array of listeners for the specified event.
6removeListener(event, listener)This removes a listener from the listener array for the specified event. Note that it will change the array indices in the listener array behind the listener. It can remove at most one instance of a listener from the listener array. 
7removeAllListeners([event])This removes all the listeners or those of the specified event. 
8setMaxListeners(n)If more than 10 listeners are added for a particular event, then, by default, EventEmitter will print a warning. This is a useful default that helps find memory leaks. setMaxListeners() allows us to increase that limit.

Frequently Asked Questions

 

  1. What is the Node.js event emitter?

The Node.js event emitter is part of the events module. It offers a way to handle our events.

 

2. How can I remove an event listener for an event?

You can use the removeListener() method to remove the event listener for an event.

 

3. How can I pass additional arguments to the event handler?

We can pass additional arguments to the event handler by passing them as additional arguments to the emit function.

 

4. Is an Event Emitter Synchronous or Asynchronous?

Event Emitter is synchronous.

Key Takeaways

The Node.js event emitter is part of the events module. It offers a way to handle our events. A lot of interactions coming from the user are handled by events. Interactions can be clicking the button, pressing buttons on the keyboard, etc. Node.js event emitter gives a  way to service these events.

 

If you enjoyed reading this article about the Node.js event emitter, check out What is Node.js? Where, When & How To Use It? and Why every web developer should learn Node.js?.

Was this article helpful ?
0 upvotes