Node.js is fundamentally an event-driven platform. Much of the Node.js core API (including HTTP servers, streams, and file system watchers) is built around the EventEmitter class located in the built-in node:events module.
An EventEmitter implements the Observer Design Pattern. Objects (called emitters) emit named events that cause previously registered functions (listeners) to be called synchronously.
| Method | Description |
|---|---|
emitter.on(eventName, listener) | Registers a persistent listener that triggers every time the event fires |
emitter.once(eventName, listener) | Registers a one-time listener that automatically removes itself after its first execution |
emitter.emit(eventName, ...args) | Synchronously calls each listener registered for the event with passed arguments |
emitter.removeListener(event, fn) | Unbinds a specific callback from an event |
emitter.setMaxListeners(n) | Increases warning threshold for listener count (default is 10 to prevent memory leaks) |
In scalable enterprise architectures, custom services inherit from EventEmitter to decouple complex domain processes:
EventEmitter class?emitter.once() instead of emitter.on()?emitter.emit('eventName') is invoked?error event is emitted on an EventEmitter and no listener is registered for 'error'?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.