What is the events module?
1. What the events module is
eventsis a built-in Node.js module implementing the event pattern (Observer / Publisher-Subscriber).
It lets you create event emitter objects (EventEmitters), which can:
- register event handlers (
on,once); - emit events (
emit); - remove handlers (
off,removeListener).
This module underlies all of Node.js's asynchronous behavior:
- HTTP servers,
- streams (
stream), - the filesystem (
fs), - network connections (
net,tls), - and even the
processobject itself.
2. Loading and creating an EventEmitter
const EventEmitter = require('events');
const emitter = new EventEmitter();Now emitter is an object that can emit and listen for events.
3. The main EventEmitter methods
| Method | Purpose |
|---|---|
on(event, listener) | Subscribe to an event |
once(event, listener) | Subscribe, but fire the handler only once |
emit(event, [...args]) | Fire (trigger) an event |
off(event, listener) / removeListener | Remove a handler |
removeAllListeners(event) | Remove every listener for an event |
listenerCount(event) | Find out how many listeners are subscribed |
eventNames() | Get the list of active events |
4. An example
const EventEmitter = require('events');
const emitter = new EventEmitter();
// Subscribe to the "message" event
emitter.on('message', (text) => {
console.log('Message received:', text);
});
// Emit the event
emitter.emit('message', 'Hello, Node.js!');Output:
Message received: Hello, Node.js!5. An example with once()
emitter.once('connect', () => {
console.log('First connection');
});
emitter.emit('connect'); // fires
emitter.emit('connect'); // ignoredonce() is handy for events that should fire only once
(for example, initializing a database connection, or starting a server).
6. An example removing listeners
function handler() {
console.log('The event happened');
}
emitter.on('event', handler);
emitter.emit('event'); // Fires
emitter.off('event', handler);
emitter.emit('event'); // Doesn't fire7. Using it via inheritance
Many built-in Node.js classes inherit from EventEmitter. You can create your own classes with the same behavior:
const EventEmitter = require('events');
class Chat extends EventEmitter {
sendMessage(msg) {
console.log('Sending:', msg);
this.emit('message', msg);
}
}
const chat = new Chat();
chat.on('message', (msg) => console.log('Received:', msg));
chat.sendMessage('Hello!');Output:
Sending: Hello!
Received: Hello!8. How it works internally
When .emit(eventName) is called:
- Node.js looks up every listener registered for
eventName; - it calls each one in turn;
- if none are found, nothing happens.
All of this runs synchronously, but it's often used to coordinate asynchronous processes (for example, notifying that I/O finished).
9. Where events is used in Node.js
| Module | Example events |
|---|---|
http.Server | 'request', 'close', 'connection' |
fs.ReadStream | 'data', 'end', 'error' |
net.Socket | 'connect', 'data', 'timeout', 'end' |
process | 'exit', 'uncaughtException', 'warning' |
child_process | 'message', 'exit' |
Summary
The
eventsmodule is the built-in foundation of Node.js's event-driven architecture. It implements theEventEmitterclass, which lets objects:
- emit events (
emit),- listen for them (
on,once),- manage subscriptions (
off,removeListener).Without it, parts of Node.js like
http,fs,stream, orprocesscouldn't work, all of them inherit from and useEventEmitter.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.