
- 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.addListener() Method
The emitter.addListener () method is used to add the listener function to the end of the array for the event named eventName. It is not possible to check if the listener has already been added to the listener array.
If multiple calls of the emitter.addListener() method passing the same combination of eventName and listener, the listener function(s) will be added to the listeners array and will be called the number of times it is added.
This method belongs to Eventemitter class is an inbuilt class of node:events module.
Syntax
Following is the syntax of the NodeJs emitter.addListener() method −
emitter.addListener(eventName, listener)
Parameters
This method accepts two parameters listed below −
- EventName: 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: A callback function executed when the specified event occurs.
Return value
It returns the instance of the EventEmitter to which the listener was added.
Example 1
First, we imported the node:events module. We created a function func1 with a message inside it. Then we called the emitter.addListener() method with an eventName passed as the first parameter and we passed the (func1) to the listener parameter of the method. Thus the function is added to the listener array. So when we compile and run the program it will print the message inside the listener function.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function func1() { console.log('Hi, im from function1'); } myEmitter.addListener('eventOne', func1); myEmitter.emit('eventOne');
Output
The above program produces the following output −
Hi, im from function1
Example 2
In the example, we created two functions and passed them to the listener function parameter. So the method myEmitter.addListener() adds the listener functions to the end of the listeners array for the event named eventOne.then it will first print the func1 and then func2.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function func1() { console.log('Hi, im from function1'); } function func2() { console.log('Hi, im from function2'); } myEmitter.addListener('eventOne', func1); myEmitter.addListener('eventOne', func2); myEmitter.emit('eventOne');
Output
The above program produces the following output −
Hi, im from function1 Hi, im from function2
Example 3
Following is another example, here we are passing the same combination of eventName and listener to the method. This will result in the listener function being added to the listener array and called multiple times. So, when we compile and run the program we can see that the func2 is added two times to listener array and printed two times on the output.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function func1() { console.log('Hi, im from function1'); } function func2() { console.log('Hi, im from function2'); } myEmitter.addListener('eventOne', func1); myEmitter.addListener('eventOne', func2); myEmitter.addListener('eventOne', func2); myEmitter.emit('eventOne');
Output
After executing the above program it will display the following output −
Hi, im from function1 Hi, im from function2 Hi, im from function2
Example 4
Note: By default, event listeners are invoked in the order they are added.
In this example, we used emitter.prependListener() method with the same eventName which we passed to the previous myEmitter.addListener() method. Then we passed a function fucn3 to the emitter.prependListener() method. So this will add the function to the beginning of the listerners array. So when we compile and run the program, the function passed inside emitter.prependListener() method will be printed first.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function func1() { console.log('Hi, im from function1'); } function func2() { console.log('Hi, im from function2'); } function func3() { console.log('Hi, im from function3 but i will be added at top the array.'); } myEmitter.addListener('eventOne', func1); myEmitter.addListener('eventOne', func2); myEmitter.prependListener('eventOne', func3); myEmitter.emit('eventOne');
Output
After executing the above program it will display the following output −
Hi, im from function3 but i will be added at top the array. Hi, im from function1 Hi, im from function2