- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js Event Emitter
The EventEmitter class is the backbone of Node.js's asynchronous, event-driven architecture. Many of Node's core modules like HTTP for servers or FS for file streams are built on top of it. It provides a way for one part of your application to "signal" that something has happened, and for other parts to "react" to that signal without being tightly coupled together.
Key Features of EventEmitter
- Custom Events: Unlike browser events (like 'click' or 'submit'), you can name your EventEmitter events anything you want, such as
user-logged-inordata-processed. - Event Listeners: These are callback functions that stay "on guard." When the specific event name is triggered, the listener executes its logic immediately.
- Synchronous Execution: By default, EventEmitter calls all listeners synchronously in the order they were registered. This ensures proper sequencing of logic.
setImmediate() or process.nextTick() inside your listener if you need the actual logic to execute asynchronously to avoid blocking the event loop.
Using EventEmitter
To use the EventEmitter, you need to import it from the built-in events module. You don't need to install anything via NPM; it's part of the Node.js core.
1. Importing EventEmitter and Creating an Event
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
// 1. Define an event and its listener
myEmitter.on('order-placed', () => {
console.log('Notification: A new order has been placed!');
});
// 2. Emit the event
myEmitter.emit('order-placed');
Output:
Notification: A new order has been placed!
- The
on()method is used to register a listener. It takes the event name (string) and a callback function. - The
emit()method triggers the event. You can call this from anywhere in your logic when a specific milestone is reached.
2. Multiple Listeners for the Same Event
One of the biggest strengths of this pattern is that multiple independent parts of your app can listen for the same event without interfering with each other.
myEmitter.on('user-signup', () => {
console.log('Sending welcome email...');
});
myEmitter.on('user-signup', () => {
console.log('Adding user to the database analytics...');
});
myEmitter.emit('user-signup');
Output:
Sending welcome email...
Adding user to the database analytics...
- Both listeners execute in the order they were defined. This is great for keeping your code modular your "Email Logic" and "Analytics Logic" can live in separate files.
emitter.setMaxListeners(n).
3. Listening Once with once()
Sometimes you only care about an event the very first time it happens. For example, you might want to run a setup script the first time a database connects, but ignore subsequent connection pulses.
myEmitter.once('db-connect', () => {
console.log('Connection established! Initializing schema...');
});
myEmitter.emit('db-connect');
myEmitter.emit('db-connect'); // This will be ignored
Output:
Connection established! Initializing schema...
- The
once()method automatically unregisters the listener after it is fired for the first time, saving memory and preventing redundant logic.
4. Handling Arguments in Events
Events rarely happen in a vacuum; you usually need to pass data along. You can pass any number of arguments to the emit() method, and they will be passed to the listener.
myEmitter.on('payment-received', (amount, currency) => {
console.log(`Success! We received ${amount} ${currency}.`);
});
myEmitter.emit('payment-received', 49.99, 'USD');
Output:
Success! We received 49.99 USD.
myEmitter.emit('event', { id: 1, status: 'ok' }).
5. Removing Event Listeners
To keep your application performant, you should remove listeners when they are no longer needed, especially in long-running applications.
const onDataReceived = () => {
console.log('Processing incoming data stream...');
};
myEmitter.on('data', onDataReceived);
myEmitter.emit('data');
// Remove the specific listener
myEmitter.removeListener('data', onDataReceived);
myEmitter.emit('data'); // This will not trigger anything
Output:
Processing incoming data stream...
myEmitter.on('event', () => { ... })). You must have a reference to the function name to remove it later.
Summary
The EventEmitter class is a powerful tool for creating decoupled, scalable systems in Node.js. By mastering on, once, and emit, you can build applications where different modules communicate seamlessly through events rather than direct function calls. This makes your code easier to test, maintain, and expand as your project grows. Remember to always clean up your listeners to prevent memory leaks in production environments!