Lesson 2/100

Tutorials Node.js Tutorial

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
Lesson 2 of 100 · Module 1: Node.js Foundations · BEGINNER
Topic: Event-Driven Architecture · Level: BEGINNER · Read time: ~12 min + hands-on

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

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.

Think of it like this: A doorbell: you do not stare at the door all day — you keep working and react when someone rings.

Why developers use this

    How it works (step by step)

    1. Register listeners with .on(eventName, handler) before or when your app starts.
    2. When something happens, call .emit(eventName, data).
    3. Every listener for that event runs — kitchen, email, SMS can all react to one payment.
    4. 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

    1. Create event-bus.js with the code above
    2. Run node event-bus.js — see two log lines
    3. Add a third listener for orderCancelled
    4. Emit both events and watch the order of logs
    Tip: Use past tense for events: userCreated, paymentFailed.

    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

    Continue learning

    Previous: Introduction to Node.js — Complete Guide

    Next: Installing Node.js — Complete Guide

    Lesson 2 of 100 · Node.js Tutorial

    Questions on this lesson 0

    Sign in to ask a question or upvote helpful answers.

    No questions yet — be the first to ask!

    Node.js Tutorial
    Course syllabus

    Node.js Tutorial

    Module 1: Node.js Foundations
    Module 2: Async Programming
    Module 3: Express.js & EJS
    Module 4: REST APIs & Databases
    Module 5: Real-Time & Event Systems
    Module 6: Advanced Node.js
    Module 7: Performance & Security
    Module 8: Testing & Deployment
    Module 9: Latest Node.js Features
    Module 10: Enterprise Projects
    Toolliyo Assistant
    Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

    care@toolliyo.com

    Need callback? Share your details