How to develop an event driven app in NodeJS
Instead of calling functions directly, you fire an event. Something else picks it up and reacts. That's event-driven programming in a sentence.
17 Apr 2024 · 2 min read

Instead of calling functions directly, you fire an event. Something else picks it up and reacts. That's event-driven programming in a sentence.
Node.js was built around this idea. The event loop, streams, HTTP servers — they all use events under the hood. The EventEmitter class is the primitive that makes it work.
Basic usage
const EventEmitter = require("events");
const emitter = new EventEmitter();
emitter.on("order:created", (order) => {
console.log(`Processing order ${order.id}`);
});
emitter.emit("order:created", { id: 1, total: 99.99 });
on registers a listener. emit fires the event. That's the entire API at its core.
A real-world pattern
Say you have an order system. When an order is placed, you need to:
- Save it to the database
- Send a confirmation email
- Update inventory
Without events, you'd stuff all three into one function. With events, each concern subscribes independently:
emitter.on("order:placed", saveToDatabase);
emitter.on("order:placed", sendConfirmationEmail);
emitter.on("order:placed", updateInventory);
emitter.emit("order:placed", orderData);
Each handler is independent. You can add, remove, or change one without touching the others.
The trade-off
Event-driven code decouples your components. That's the benefit.
The cost is traceability. When something goes wrong, you can't just follow the call stack. You have to trace which listeners are registered, in what order, and whether any of them threw. Debugging event-driven systems is harder than debugging direct function calls.
Also, EventEmitter is in-process only. If you need events across services, you need a message broker like Redis Pub/Sub, RabbitMQ, or Kafka. The EventEmitter pattern stays the same, but the infrastructure underneath changes completely.
Use events when you want loose coupling and multiple reactions to a single trigger. Use direct calls when the flow is linear and debuggability matters more than flexibility.
Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.
Forward Deployed AI Engineering: From Customer Problem to Production Value
A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.
January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.
View the live cohortKeep reading
- TypeScript Anti-Patterns That Cost You Twice: Build Time, Runtime, and the Interview
- The Frontend Toolchain Is Now Written in Rust and Go. What That Means for You
- oxlint vs ESLint: Why I Replaced ESLint with oxlint
- SystemJS Is Dead. Native ESM Finally Won.
- Replace Axios with a Simple Custom Fetch Wrapper (Production-Ready Guide)
- Stop Shipping ChatGPT Wrappers. Ship an Agent in TypeScript, or Don't Bother.