Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024

The events module in Node.js

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM



Every action on a computer is like an event. For, e.g., when a connection is made, or you open any file. Objects inside Node.js can fire events, like the readStream object fires events when opening or closing a file.

Node.js is an asynchronous event-driven JavaScript runtime. Its event-driven architecture allows it to perform asynchronous tasks. It has a built-in module called the 'events' module, where you can create, fire, and listen for your events. When a function (Callback) listens to a particular event to occur, all the callbacks subscribing to that event are fired one by one in the sequence in which they were registered.

All the objects that emit events are instances of the EventEmitter class.

The EventEmitter class

The events module provides an EventEmitter class, which is the key to working with events in Node.jsThe event can be emitted or listened to only with the help of EventEmitter.



const EventEmitter = require('events');
var eventEmitter = new EventEmitter.EventEmitter();


The EventEmitter is a class. By convention, it is in camel casing. All event properties and methods are the instances of an EventEmitter object. You can access them by creating an object of the EventEmitter class called eventEmitter.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

The EventEmitter Object

An EventEmitter object has two functions:

  1. Emit a named event.
  2. Attach or detach listeners to the named event.

You can assign event handlers to the events with the EventEmitter object.

For example, we have created a function executed when a "scream" event is fired. To fire an event, use the emit() method. 

var event = require('events');
var eventEmitter = new event.EventEmitter();

//Create an event handler
var handler = function () {
  console.log('I hear a scream!');

//Assign the event handler to an event
eventEmitter.on('scream', handler);

//Fire the event:





Listening events:

The event listener has these in-built events:

  • newListener: Add a new listener.
  • removeListener: Remove a listener.

Methods in the EventEmitter class

  • eventEmitter.addListerner()


eventEmitter.addListener(event, listerner)

It is an alias for the eventEmitter.on().


  • eventEmitter.emit()


eventEmitter.emit("eventName"// pass the event’s name 


This method emits an event. It will synchronously call every event listener in the order they were registered.


  • eventEmitter.eventNames()




Returns an array of strings that represent the events registered on the current eventEmitter object.


  • eventEmitter.getMaxListeners()




Give the maximum amount of listeners that can be added to an EventEmitter object which by default is 10 but can be increased or decreased by using setMaxListeners().c.opy


  • eventEmitter.listenerCount()




Returns the number of listeners of the event passed as a parameter.


  • eventEmitter.listeners()




Returns an array of listeners of the event that is passed as a parameter.



It is an Alias for eventEmitter.removeListener() added in Node.js 10.

  • eventEmitter.on()


eventEmitter.on('open', () => {
    console.log('Coding Ninjas!')


It adds a callback function that is called when an event is emitted.


  • eventEmitter.once()



eventEmitter.once('my-event', () => {
 //call callback function once


It adds a callback function when an event is emitted for the first time after registering. This callback is never going to be called again.


  • eventEmitter.prependListener()

When you use on or addListener to add a listener, it's called last since it's added last in the listener's queue. But using prependListener, it's added and called before other listeners.

  • eventEmitter.prependOnceListener()

When you use once to add a listener, it's called last since it's added last in the listener's queue. But using prependOnceListener, it's added and called before other listeners.

  • eventEmitter.removeAllListeners()




It removes all listeners of the EventEmitter object listening to a specific event.


  • eventEmitter.removeListener()


const doSomething = () => {}
eventEmitter.on('open', doSomething)
eventEmitter.removeListener('open', doSomething)


It removes a specific listener by saving the callback function to a variable when added so that you can reference it later.


  • eventEmitter.setMaxListeners()




It sets the maximum number of listeners added to an EventEmitter object but can be increased or decreased by default.

Asynchronous events: 

The EventEmitter object calls the listeners synchronously in the order in which they were registered. However, we can perform asynchronous calls on the listeners using setImmediate() or process.nextTick().

Frequently Asked Questions


Q1: What is the event module?


Ans: It is a Node.js module that provides you with an EventEmitter class that allows you to manage the events in the node application. 


Q2: What is node js event emitter?


Ans: The EventEmitter is a class of ‘events’ mode in Node.js. It contains functions and methods that facilitate communication/interaction between objects.


Q3: What is the difference between events and callbacks in node JS?


Ans: In NodeJS, events are the same as for callbacks. A callback function is called to fire events based on the observer when a function's execution is complete. Every event has listeners, and when an event is triggered, the listener function associated with it begins the execution process.


Q4: Is the event listener a callback?


Ans: The event listener can be a callback function or an object that implements EventListener and has a handleEvent() method that acts as a callback function.

Key takeaways

In this blog, we learned about the ‘events’ module in node.js. We saw the methods and their syntax provided by EventEmitter class of the module.


Coding Ninjas provides a whole course on NodeJS. It provides basic to Advance level understanding. Enroll today and build amazing websites using Node. Click on the given link to enroll.

Topics covered
The EventEmitter class
The EventEmitter Object
Listening events:
Methods in the EventEmitter class
Asynchronous events: 
Frequently Asked Questions
Key takeaways