Understanding EventEmitter in Node.js With a UseCase

In this article, we will see what are event emitters and how it works by understanding some use cases. Understanding EventEmitter in Node.js With a UseCase

Recent Articles about Node.js,

Apache Kafka for Node.js Developers

One of the core concepts of Node.js is events. Event Emitter plays a vital role in Node.js Event-Driven Architecture

Event Emitter is a module that facilitates communication between objects in Node.js. Most of the Libraries/Modules built on Node.js uses EventEmitter Since Node.js follows the Event-Driven Architecture.

That is to say, we will learn Event Emitter by building a custom logger using Event Emitter in this article.

How does it work?

  • Event Emitter emits the data in an event called message
  • A Listened is registered on the event message
  • when the message event emits some data, the listener will get the data.

eventEmitter works

Building Blocks

  • .emit() - this method in event emitter is to emit an event in module
  • .on() - this method is to listen to data on a registered event in node.js
  • .once() - it listen to data on a registered event only once.
  • .addListener() - it checks if the listener is registered for an event.
  • .removeListener() - it removes the listener for an event.


1const EventEmitter = require("events")
3const emitter = new EventEmitter()
5emitter.on("message", function(message) {
6 console.log("subsribed", message)
9emitter.emit("message", { data: "Data" })

Firstly, import the package called events which is basically a class. After that, instantiates the class with a name emitter.

After that, we need to register the listener for the particular event.

Finally, when if we emit an event, the registered listener will get the event along with the data.


As I said earlier, event emitter is an important concept in node.js. we learned how it works and important building blocks of it.

But, One of the important questions that come to our mind when we learn something is, where do I need to implement that?.

Let's see a use-case to understand more about Event-Emitters.

Logger using Event Emitter

Mainly, we will see how to write a logger function using Event Emitter.

create a file called logger.js and add the following code.

1const EventEmitter = require("events")
3class Logger extends EventEmitter {
4 logIt(eventName, message) {
5 this.emit(eventName, message)
6 }
9module.exports = Logger

class Logger extends the EventEmitter class. add the following method called logIt which takes event name and message as an argument.

create a file called an index.js and add the following code.

1const Logger = require("./Logger")
3const logger = new Logger()
5logger.on("logMessage", arg => {
6 console.log("Listened value => ", arg)
9logger.logIt("logMessage", { name: "John", age: 40 })

it requires the module Logger and instantiates the class as a logger.

logIt() function emits event called logMessage. the listener will listen for the event and log it.

To Learn some Advanced concepts in Node.js, One of my favorite courses is from Stephen Grider. I recommend this course to learn advanced concepts in Node.js

Node JS: Advanced Concepts

To Read More

Kubernetes for Nodejs developers

Do you keep hearing the word kubernetes in the tech community and you couldn't u...

TypeScript Interfaces vs Types

In this article, we will see what are interfaces and types and the difference be...

How to find project ideas to practi...

Ever wondered what how to get a real world experience on web development while w...

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...