
- 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.getMaxListeners() Method
The emitter.getMaxListeners() method will help us to return the current maximum listener limit value for the eventEmitter which is set by emitter.setMaxListeners(n).
This method will do nothing more than return the maximum listener limit of the eventEmitter.
This method belongs to Eventemitter class is an inbuilt class of node:events module.
Syntax
Following is the syntax of the NodeJs emitter.getMaxListeners() method −
emitter.getMaxListners()
Parameters
- This method does not accept any parameters.
Return value
This method will return the count of listener functions listening to the event named eventName.
Example 1
Following is the basic example of the NodeJs emitter.getMaxListeners() Method.
Initially, we imported the node:events module. Then we passed a single listener function f1() with an eventName myEvent to the emitter.addListener() method. Then we called emitter.getMaxListeners() method by passing myEvent as a parameter. It returns 10 because By default, the EventEmitters will accept only 10 listeners for a particular event.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ }; myEmitter.addListener('myEvent', f1); console.log(myEmitter.getMaxListeners());
Output
The above program produces the following output −
10
Example 2
In this program we again passed a single listener function f1() with a eventName myEvent to the emitter.addListener() method. Then we called the emitter.setMaxListeners() method by modifying the default limit by 1.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ }; myEmitter.setMaxListeners(1); myEmitter.addListener('myEvent', f1); console.log(myEmitter.getMaxListeners());
Output
The above program produces the following output −
1
Example 3
In this program, we set the listener limit as 1 to the specific event myEvent and we are adding 4 listeners to the same event named myEvent. So, the EventEmitter will throw a warning message that memory leak detected.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ }; function f2(){ }; function f3(){ }; function f4(){ }; myEmitter.setMaxListeners(1); myEmitter.addListener('myEvent', f1); myEmitter.addListener('myEvent', f2); myEmitter.addListener('myEvent', f3); myEmitter.addListener('myEvent', f4); console.log(myEmitter.getMaxListeners());
Output
The above program produces the following output −
1 (node:33292) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 2 myEvent listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit (Use `node --trace-warnings ...` to show where the warning was created)