Skip to main content

What is the events module?

1. What the events module is

events is 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 process object itself.

2. Loading and creating an EventEmitter

javascript
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

MethodPurpose
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) / removeListenerRemove 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

javascript
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:

javascript
Message received: Hello, Node.js!

5. An example with once()

javascript
emitter.once('connect', () => { console.log('First connection'); }); emitter.emit('connect'); // fires emitter.emit('connect'); // ignored

once() is handy for events that should fire only once (for example, initializing a database connection, or starting a server).

6. An example removing listeners

javascript
function handler() { console.log('The event happened'); } emitter.on('event', handler); emitter.emit('event'); // Fires emitter.off('event', handler); emitter.emit('event'); // Doesn't fire

7. Using it via inheritance

Many built-in Node.js classes inherit from EventEmitter. You can create your own classes with the same behavior:

javascript
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:

javascript
Sending: Hello! Received: Hello!

8. How it works internally

When .emit(eventName) is called:

  1. Node.js looks up every listener registered for eventName;
  2. it calls each one in turn;
  3. 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

ModuleExample 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 events module is the built-in foundation of Node.js's event-driven architecture. It implements the EventEmitter class, 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, or process couldn't work, all of them inherit from and use EventEmitter.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.