
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
NodeJS - emitter.listenerCount() Method
The emitter.listenerCount() method is used to retreive the number of listeners listening to the event (eventName) which we passed 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.listenersCount() method −
emitter.listenerCount(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
(integer) This method will return the count of listener function(s) which are listening to the event (eventName) that we pass as a parameter.
Now lets get into the examples of the Node.js eventemitter.listeners() method in different scenarios.
Example 1
Following is the basic example of the NodeJs emitter.ListenersCount() Method.
In this program, We created a listener function listener() with a message inside it. Then we called the emitter.on() method with an eventName (event) passed as the first parameter and we passed the (listener()) to the listener parameter of the method and logged the count of the listeners which are listening to the event named event.
const EventEmitter = require('node:events'); const myEvent = new EventEmitter(); function one(){ console('January'); }; myEvent.on('event', one); console.log(myEvent.listenerCount('event'));
Output
The above program produces the following output −
1
Example 2
We created multiple functions one(), two(), three(), and four(). Then we called the emitter.on() method multiple times with the same eventName (event) passed as the first parameter and we passed the listener functions created before to the listener parameter of the method. Then we passed the event to the myEvent.listenerCount() method as a parameter. When we compile and run the program, the myEvent.listenerCount() method will return the count of listeners for the event named event.
const EventEmitter = require('node:events'); const myEvent = new EventEmitter(); function one(){ console('January'); }; function two(){ console('February'); } function three(){ console('March'); } myEvent.on('event', one); myEvent.on('event', two); myEvent.on('event', three); console.log(myEvent.listenerCount('event'));
Output
The above program produces the following output −
3
Example 3
We passed one listener function to event and three listener functions to event1. So, we called myEmitter.listenerCount() with both event and event1. When we compile and run the program, the myEvent.listenerCount('event') will return the count as 1 and myEvent.listenerCount('event1') will return the count as 2.
const EventEmitter = require('node:events'); const myEvent = new EventEmitter(); function one(){ console('January'); }; function two(){ console('February'); } function three(){ console('March'); } myEvent.on('event', one); myEvent.on('event1', two); myEvent.on('event1', three); console.log(myEvent.listenerCount('event')); console.log(myEvent.listenerCount('event1'));
Output
The above program produces the following output −
1 2