Suggest an editImprove this articleRefine the answer for “What is an event in the context of Node.js?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)An **event** is a signal that something happened (data arrived, a file read finished, a client connected); Node.js is built around an event-driven model, where code reacts to events via `EventEmitter` instead of blocking while waiting for them to finish. **Key point:** every asynchronous operation in Node.js (files, network, streams, timers) ultimately produces an event that the Event Loop handles.Shown above the full answer for quick recall.Answer (EN)Image## 1. What an event is in Node.js > An **event** is a **signal that something happened** > (for example, data arrived, a file read finished, a client connected, and so on). Node.js is built around an **event-driven architecture**, where code **reacts to events**, rather than blocking while waiting for them to finish. ## 2. The events mechanism: EventEmitter At its core is the `EventEmitter` class from the `events` module. It lets objects: - **emit events** (`emit`); - **listen for events** (`on`). Example: ```javascript const EventEmitter = require('events'); const emitter = new EventEmitter(); // Subscribe to an event emitter.on('greet', (name) => { console.log(`Hello, ${name}!`); }); // Emit the event emitter.emit('greet', 'Tim'); ``` Here: - `'greet'` is the event name, - `on` is the listener, - `emit` fires the event, - the callback passed to `.on()` gets called. ## 3. How this ties into I/O Node.js uses events for every **asynchronous operation**: - network requests (`http`, `net`), - the filesystem (`fs`), - streams, - timers (`setTimeout`, `setInterval`). For example: ```javascript const fs = require('fs'); const stream = fs.createReadStream('file.txt'); stream.on('data', chunk => console.log('Got a chunk')); stream.on('end', () => console.log('File read')); ``` Here: - the stream **emits events** (`data`, `end`, `error`), - and you **react to them**, instead of waiting for synchronous completion. ## 4. The Event Loop's role Every event is processed through the **Event Loop**, the "engine" that: 1. watches the event queue; 2. calls registered callbacks when an event happens; 3. never blocks (everything is non-blocking). An example lifecycle: ```javascript [Incoming data] → an event fires → callback → processing → the next cycle ``` ## 5. Where events show up | Object | Typical events | |---|---| | `fs.ReadStream` | `data`, `end`, `error`, `close` | | `http.Server` | `request`, `connection`, `close` | | `net.Socket` | `connect`, `data`, `end`, `error` | | `process` | `exit`, `uncaughtException`, `SIGINT` | | `EventEmitter` | any custom ones (`'ready'`, `'updated'`, and so on) | ## 6. Why the event model is efficient - **Asynchrony with no blocking**: the thread doesn't wait, events fire once the result is ready. - **High scalability**: thousands of connections can be served on one thread. - **Simple reaction logic**: instead of a wait loop, subscribing to events. ## Summary > An **event** in Node.js is a notification that a specific action occurred. > > It's handled through the **EventEmitter** mechanism, and its execution is driven by the **Event Loop**. > > All of Node.js's asynchrony, file operations, HTTP, streams, timers, > is built on the **event model**, where code **reacts** to events instead of **waiting** for them to finish.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.