EventEmitter Pattern
The Nucleus of Node: EventEmitter Pattern
The Observer design pattern is baked into the core DNA of Node.js via the EventEmitter class. Almost every significant native module—HTTP servers, File Streams, Child Processes—inherits from EventEmitter to manage asynchronous state changes.
Decoupling Architecture
EventEmitters allow disparate parts of a monolithic codebase to communicate without tight coupling. A "Publisher" emits an event indiscriminately, and one or many "Subscribers" execute corresponding logic asynchronously.
Critical Conventions
- The 'error' Event: EventEmitter possesses a special rule. If an 'error' event is emitted but no
.on('error')listener exists to catch it, Node.js escalates it to an unhandled exception and crashes the process. - Memory Leaks: By default, binding more than 10 listeners to a single event triggers a memory leak warning. Use
setMaxListeners()deliberately.