Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NODENode.js

    Runtime and core tooling

    • What is Node.js?
    • CommonJS and ES modules
    • npm and package.json
    • process and command-line apps

    Async I/O and events

    • Event loop: callbacks to async/await
    • Files and paths with fs/promises
    • EventEmitter
    • Streams and backpressure

    HTTP and APIs

    • Your first HTTP server
    • Routing and JSON bodies
    • Build a tiny router and middleware chain
    • REST: resources and status codes
    • Validation and error handling

    Production: security, testing and deployment

    • Authentication basics with crypto
    • Configuration and environment secrets
    • Testing with assert and node:test
    • API security fundamentals
    • Project: tasks REST API and deployment
    Node.js

    Async I/O and events · Lesson 7 of 18

    EventEmitter

    Decouple producers from listeners, and understand synchrony and listener cleanup.

    10 minutes2 games

    EventEmitter implements the Observer pattern: a producer emits a named event and data, while many listeners react without the producer knowing them. It suits in-process events such as order:created, not cross-server messaging.

    Producer

    emit(order)

    Email listener

    Analytics listener

    Node.js
    const { EventEmitter } = require("node:events")
    const bus = new EventEmitter()
    bus.on("order", (id) => console.log("email", id))
    bus.once("order", (id) => console.log("first-order", id))
    bus.emit("order", 42)
    bus.emit("order", 43)

    Listeners run synchronously in registration order inside emit. If a listener throws and nobody handles it, emit throws. Use once for one-shot work and off when a lifecycle ends to prevent leaks.

    Important

    The error event is special: emitting it without a listener makes Node throw and may terminate the process. Define an error strategy, and do not let a slow listener block all others.

    Quick question

    How often does a once listener run after two emits?

    Interview question

    How does EventEmitter differ from a queue such as RabbitMQ?

    Exercise

    Create an emitter that totals score events and prints 12 after 5 and 7.

    Expected output: 12

    Node.js
    const { EventEmitter } = require("node:events")
    const bus = new EventEmitter()
    let total = 0
    
    bus.emit("score", 5)
    bus.emit("score", 7)
    console.log(total)

    Learn more from

    • Node.js: Eventsnodejs.org
    • MDN: EventTargetdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousFiles and paths with fs/promisesNextStreams and backpressure
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.