Node.js Events & EventEmitter

Node js 8 min min read Updated: Mar 29, 2026 Beginner
Node.js Events & EventEmitter
Beginner Topic 8 of 12

Node.js Events & EventEmitter

Node.js is built on an event-driven architecture, which means many operations are triggered by events. Instead of executing code step by step in a blocking manner, Node.js listens for events and responds when those events occur.

This approach makes Node.js highly efficient and scalable, especially for applications that handle multiple users at the same time such as APIs, chat applications, and real-time systems.

Key Concept: In Node.js, events allow different parts of your application to communicate without being tightly connected.

What is EventEmitter?

The EventEmitter class is a core module in Node.js that allows you to create and handle custom events. It is part of the built-in events module.

With EventEmitter, you can:

  • Create custom events
  • Listen for events
  • Trigger (emit) events

Importing EventEmitter

javascript const EventEmitter = require("events"); const emitter = new EventEmitter(); emitter.on("greet", () => console.log("Hello")); emitter.emit("greet");

In this example:

  • require("events") imports the EventEmitter class
  • new EventEmitter() creates an instance
  • emitter.on() listens for an event
  • emitter.emit() triggers the event
Output

Hello

Understanding Event Flow

The working of EventEmitter follows a simple pattern:

  • Register Listener: Use on() to listen for an event
  • Emit Event: Use emit() to trigger the event
  • Execute Callback: The function runs when the event occurs

Passing Data with Events

You can pass data when emitting an event, making it more dynamic:

javascript emitter.on("greet", (name) => { console.log("Hello " + name); }); emitter.emit("greet", "Rahul");

This will output:

Output

Hello Rahul

Multiple Listeners

You can attach multiple listeners to the same event:

javascript emitter.on("event", () => console.log("Listener 1")); emitter.on("event", () => console.log("Listener 2")); emitter.emit("event");

All listeners will execute when the event is emitted.

Using once() Method

The once() method ensures that the event listener runs only one time.

javascript emitter.once("login", () => console.log("User logged in")); emitter.emit("login"); emitter.emit("login");

The output will appear only once, even though the event is emitted twice.

Removing Event Listeners

You can remove listeners using off() or removeListener():

javascript const callback = () => console.log("Event triggered"); emitter.on("test", callback); emitter.off("test", callback);

Real-World Use Cases

  • Handling HTTP requests and responses
  • Real-time chat applications
  • File streaming and processing
  • Custom logging systems
  • Notification systems

Why EventEmitter is Important

EventEmitter allows different parts of your application to communicate without directly depending on each other. This leads to better scalability and cleaner architecture.

It is one of the core reasons why Node.js can handle large-scale, real-time applications efficiently.

Common Mistakes

  • Emitting events before listeners are registered
  • Not handling errors in event callbacks
  • Adding too many listeners (can cause memory leaks)

Best Practices

  • Always register listeners before emitting events
  • Use meaningful event names
  • Remove unused listeners to avoid memory leaks
  • Handle error events properly

Conclusion

Node.js Events and EventEmitter provide a powerful way to build asynchronous and scalable applications. By using events, you can create loosely coupled systems where different components communicate efficiently.

Understanding EventEmitter is essential for mastering Node.js, as it forms the backbone of many internal and external modules.

Quick Summary: EventEmitter allows you to create, listen, and trigger events in Node.js using on() and emit().

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators