Stop 02 · Node.js FundamentalsTopics · 16/18

Event Emitter

Part of the Backend Developer Roadmap

In short

Node's servers, streams and sockets all announce events like 'request', 'data' and 'close'. EventEmitter is the small class behind that, and your own code can use it too.

Node's built-in pattern for building objects that emit named events other code can listen for — the mechanism underlying HTTP servers, streams, and much of Node's core API.

The mascot rings a big brass bell while kids, a canteen worker and a guard all react.

Think of it like this

The school bell

The peon rings the bell once. Kids rush out, the canteen opens its shutter, the guard opens the gate. The peon doesn't know or care who's listening. That's an event emitter: one emit, many listeners, each doing its own job.

Watch it happen

One event, two listeners

Order code
Emitter
Email
Stock

on("placed")

1/6At startup, the email module registers a listener for the 'placed' event.

The big ideas

What you need to know

on, once, off, emit

on adds a listener, once adds one that runs a single time, off removes one, emit fires the event.

Synchronous, in order

Listeners run one after another inside emit(), in the order they were registered.

'error' is special

Emitting 'error' with no listener throws, which can crash the process. Always listen for it.

It's everywhere in Node

http.Server, streams, child processes and process itself are all EventEmitters.

See it in code

Walk through the code

js
1import { EventEmitter } from "node:events";2 3class Orders extends EventEmitter {4  place(order) {5    // ...save to the database here...6    this.emit("placed", order);7  }8}9 10const orders = new Orders();11orders.on("placed", (o) => console.log("Email receipt to", o.email));12orders.on("placed", (o) => console.log("Reduce stock for", o.item));13orders.once("placed", () => console.log("First order of the day!"));14orders.on("error", (err) => console.error("Order error:", err.message));15 16orders.place({ email: "asha@example.com", item: "Notebook" });

1/4Your class extends EventEmitter and announces 'placed' after saving.

Listeners run in the order they were added, inside emit(). Place a second order and 'First order of the day!' won't print again.

Save yourself the pain

Common mistakes

Oops

Emitting 'error' with no error listener attached.

Do this instead

Node throws it and the process can crash. Add an 'error' listener to every emitter that can fail.

Oops

Adding a listener inside a request handler, on every request.

Do this instead

Listeners pile up and leak memory, and Node warns after 10 on one event. Register them once at startup.

Your mission

An event-driven signup

~20 min

Quick check

An EventEmitter has three listeners for 'placed'. What happens when you call emit("placed")?

Go deeper

  1. 1Build a simple custom class extending `EventEmitter` that emits a custom event.
  2. 2Learn `.on()`, `.once()` and `.emit()`.
  3. 3Recognize that Node's `http.Server` and streams are themselves EventEmitters.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge