
- 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.on() Method
The emitter.on() method is used to add the function(s) which are passed as listener function to the end of the listener array for the event named eventName. There is no possible way to check if the listener has already been added to the listener array.
If multiple calls of the emitter.on() 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.on() method −
eventEmitter.on(eventName, listener)
Parameters
This method accepts two parameters listed below −
- EventName: It is a string or a symbol that uniquely identifies the event.
- Listener:It is a callback function that will be executed when the specified event is emitted.
Return value
It returns an event listener for a specific event. When the specified event is emitted, the provided callback function is executed.
Example 1
The following is the basic example of the NodeJs emitter.on() Method.
Initially, we imported the node:events module. we created a function func1 with a message inside it. Then we called the emitter.on() 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('Welcome peeps!'); } myEmitter.on('eventOne', func1); myEmitter.emit('eventOne');
Output
The above program produces the following output −
Welcome peeps!
Example 2
In this example, we created two functions and passed them to the listener function parameter. So the method myEmitter.on() adds the listener functions to the end of the listeners array for the event named eventOne. So when we compile and run the program it will first print the func1 and then func2.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function func1() { console.log('Welcome peeps!'); } function func2() { console.log('Hope you have a good day.'); } myEmitter.on('eventOne', func1); myEmitter.on('eventOne', func2); myEmitter.emit('eventOne');
Output
The above program produces the following output −
Welcome peeps! Hope you have a good day.
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 and printed two times on the output.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function func1() { console.log('Welcome peeps!'); } function func2() { console.log('Hope you have a good day.'); } myEmitter.on('eventOne', func1); myEmitter.on('eventOne', func2); myEmitter.on('eventOne', func2); myEmitter.emit('eventOne');
Output
The above program produces the following output −
Welcome peeps! Hope you have a good day. Hope you have a good day.
Example 4
In this example, we used emitter.prependListener() method with the same eventName which we passed to the previous myEmitter.on() method. Then we passed a function fucn3 to the emitter.prependListener() method. So this will add the function to the beginning of the listeners 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('Welcome peeps!'); } function func2() { console.log('Hope you have a good day.'); } function func3() { console.log('This will be added first'); } myEmitter.on('eventOne', func1); myEmitter.on('eventOne', func2); myEmitter.prependListener('eventOne', func3); myEmitter.emit('eventOne');
Output
The above program produces the following output −
This will be added first Welcome peeps! Hope you have a good day.