Event-Driven Architecture — Complete Guide
Event-Driven Architecture — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Node.js Tutorial on Toolliyo Academy.
On this page
Event-Driven Architecture
Node is built around events. Instead of doing everything in one long line, your program listens for things that happen and reacts.
What you will learn
- What event-driven architecture means — in normal words, not textbook words
- How it works step by step
- Code you can run today on your laptop
- Where teams use this in real projects
Before you start
- Software: Node.js LTS from nodejs.org, VS Code, and a terminal
- Knowledge: Lessons 1–1 in this course
- Previous lesson: Introduction to Node.js — Complete Guide
Explain it simply
Event-driven means: when something happens (file read, user connects, payment completes), you run a function in response. The rest of your program can keep moving.
Why developers use this
How it works (step by step)
- Register listeners with
.on(eventName, handler)before or when your app starts. - When something happens, call
.emit(eventName, data). - Every listener for that event runs — kitchen, email, SMS can all react to one payment.
- The emitter does not wait for slow listeners unless you design it that way.
Code example — type this yourself
const EventEmitter = require('events');
const orderBus = new EventEmitter();
// Listen
orderBus.on('orderPlaced', (orderId) => {
console.log('Kitchen received order:', orderId);
});
orderBus.on('orderPlaced', (orderId) => {
console.log('Email receipt for order:', orderId);
});
// Fire event once — two listeners run
orderBus.emit('orderPlaced', 'ORD-42');
on registers a listener. emit triggers all listeners for that event name. One event can wake up many parts of your app.
What each part does
const EventEmitter = require('events');— Loads a built-in module or package you installed with npm.const orderBus = new EventEmitter();— Line 2: runs as written.// Listen— Line 3: runs as written.orderBus.on('orderPlaced', (orderId) => {— Event pattern: listen with on, trigger with emit.console.log('Kitchen received order:', orderId);— Prints to the terminal — great for learning; use proper logging in production.});— Line 6: runs as written.orderBus.on('orderPlaced', (orderId) => {— Event pattern: listen with on, trigger with emit.console.log('Email receipt for order:', orderId);— Prints to the terminal — great for learning; use proper logging in production.
Real life: food delivery app
When a customer pays, the payment service emits orderPaid. The kitchen service prints the ticket. The SMS service texts the customer. Payment code does not need to know about kitchens or SMS — it just emits one event.
Try it yourself — hands-on
- Create
event-bus.jswith the code above - Run
node event-bus.js— see two log lines - Add a third listener for
orderCancelled - Emit both events and watch the order of logs
Common mistakes (avoid these)
- Calling emit before on — the listener must be registered first (or use once for one-shot handlers).
Interview note
Interviewers often ask: “What is Event-Driven Architecture?” Answer in one sentence, then give a tiny example you actually ran.
Summary
- Events decouple parts of your application
- EventEmitter is built into Node
- Name events clearly: orderPlaced not data
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!