NodeJs - emitter.removeListener() Method


The emitter.removeListener() method is used to remove the specified listener function from the listener array of the event named eventName.

This method will remove the most recently added instance of a listener function from the listener array. If in case one listener is added multiple times to the listener array of the event named eventName, then the removeListener() method should be called multiple times to remove each instance from the listener array.

This method belongs to Eventemitter class is an inbuilt class of node:events module.

Syntax

Following is the syntax of the NodeJs emitter.removeListener() method −

emitter.removeListener(eventName, listener)

Parameters

This method accepts two parameters listed below −

  • EventName: (required) This is the first parameter of the method and it holds the name of the event. It can be either string or a symbol.
  • Listener: (required) This parameter holds the callback function.

Return value

This method returns a reference to the EventEmitter so that calls can be chained.

Now lets get into the examples of the emitter.removeListener() method of Node.js in different scenarios.

Example 1

Following is the basic example of the NodeJs emitter.removeListener(event, listener) Method.

We created a function f1() with a message inside it. Then we called the emitter.addListener() method with an eventName (event) passed as the first parameter and we passed the (f1) to the listener function parameter of the method. Thus the function is added to the listener array. Then we called myEmitter.removeListener() method with eventName (event) and f1() function. So when we compile and run the program myEmitter.removeListener() method will remove the listener function f1() of event named event.

const EventEmitter = require('node:events');

const myEmitter = new EventEmitter();

function f1(){
    console.log('I\'m from function1');
}

myEmitter.addListener('event', f1);

myEmitter.removeListener('event', f1);

myEmitter.emit('event');

Output

The above program produces the following output −

Program did not output anything!

Example 2

In this program, we are passing the same combination of eventName and listener function to the myEmitter.addListener() method. This will result in the listener function being added to the listener array and called multiple times.

const EventEmitter = require('node:events');

const myEmitter = new EventEmitter();

function f1(){
    console.log('I\'m from function1');
}

myEmitter.addListener('event1', f1);
myEmitter.addListener('event2', f1);
myEmitter.addListener('event3', f1);
myEmitter.addListener('event4', f1);

myEmitter.removeListener('event1', f1);
myEmitter.removeListener('event2', f1);

myEmitter.emit('event3');
myEmitter.emit('event4');

Output

The above program produces the following output −

I'm from function1
I'm from function1

Example 3

Note: By default, event listeners are invoked in the order they are added.

We added a single listener function (f1) multiple times for a single event (event). Then we called the myEmitter.removeListener() with an event named event and listener f1().

const EventEmitter = require('node:events');

const myEmitter = new EventEmitter();

function f1(){
    console.log('Hi');
}

myEmitter.addListener('event', f1);
myEmitter.on('event', f1);
myEmitter.once('event', f1);

myEmitter.removeListener('event', f1);

myEmitter.emit('event');

Output

The above program produces the following output −

Hi   
Hi
nodejs_events.htm
Advertisements