NodeJS - emitter.listeners() Method


The emitter.listeners() method will return a copy of the array of listener function(s) for the event (eventName) which we pass as a parameter.

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

Syntax

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

emitter.listeners(eventName)

Parameters

This method accepts only one parameter as described below.

  • eventName: This parameter of the method will hold the name of the event. It can be either string or a symbol.

Return value

(Function[]) This method returns a copy of the array of listeners for the event (eventName) which we pass as a parameter.

Example 1

Following is the basic example of the NodeJs emitter.Listeners() Method.

Here, we created a function f1() with a message inside it. Then we called the emitter.on() method with an eventName (myEvent) passed as the first parameter and we passed the (f1) to the listener parameter of the method. So, it returns a copy of the array of listeners for the event named myEvent.

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

function f1() {
};

myEmitter.on('myEvent', f1);

console.log(myEmitter.listeners('myEvent'));

Output

The above program produces the following output −

[ [Function: f1] ]

Example 2

In this program, we created multiple functions f1(), f2(), and f3(). Then we called the emitter.on() method multiple times with same eventName (myEvent) passed as the parameter and we passed the listener functions created before to the listener parameter of the method. Then we passed myEvent to the emitter.listeners() method as parameter. So, it returns a copy of the array of listeners for the event named myEvent.

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

function f1() {
};

function f2() {
};

function f3() {
};

myEmitter.on('myEvent', f1);
myEmitter.on('myEvent', f2);
myEmitter.on('myEvent', f3);

console.log(myEmitter.listeners('myEvent'));

Output

The above program produces the following output −

[ [Function: f1], [Function: f2], [Function: f3] ]

Example 3

In this program, we passed one listener function to myEvent and three listener functions to myEvent1. So, we called myEmitter.listeners() with both myEvent and myEvent1. When we compile and run the program, the myEmitter.listeners('myEvent') will return only one listener function and myEmitter.listeners('myEvent1') will return three listener functions.

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

function f1() {
};

function f2(){
};

function f3(){
};

function f4(){
};

myEmitter.on('myEvent', f1);
myEmitter.on('myEvent1', f2);
myEmitter.on('myEvent1', f3);
myEmitter.on('myEvent1', f4);

console.log(myEmitter.listeners('myEvent'));
console.log(myEmitter.listeners('myEvent1'));

Output

The above program produces the following output −

[ [Function: f1] ]
[ [Function: f2], [Function: f3], [Function: f4] ]
nodejs_events.htm
Advertisements