NODEJS Contents

events & EventEmitter: Patterns and Pitfalls

EventEmitter is Node's core abstraction for decoupling modules, but must be managed to avoid leaks and unbounded listener growth.

On this page

Event-Driven Foundations

The EventEmitter class underpins Node's architecture. Streams, HTTP servers, and sockets are built on top of it.

import { EventEmitter } from 'events';

const emitter = new EventEmitter();
emitter.on('ready', () => console.log('System ready'));
emitter.emit('ready');

Production Patterns

Events enable loose coupling between modules. Avoid global emitters and uncontrolled listener growth which can cause memory leaks.

Operational Insight

Monitor listener counts in long-running services to prevent leaks and unexpected behavior.