Suggest an editImprove this articleRefine the answer for “What is the events module?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)`events` is Node.js's built-in module implementing the event pattern (Observer / Publisher-Subscriber) via the `EventEmitter` class: registering handlers (`on`, `once`), emitting events (`emit`), and removing handlers (`off`, `removeListener`). **Key point:** this module underlies nearly all of Node.js's asynchronous behavior - `http`, `stream`, `fs`, `net`, even the `process` object itself, inherit from `EventEmitter`.Shown above the full answer for quick recall.Answer (EN)Image## 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 | 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 ```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 | 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 `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`.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.