Suggest an editImprove this articleRefine the answer for “What does the event model look like?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The **event model** is an architectural principle where a program doesn't run tasks sequentially, but reacts to events; in Node.js this is implemented through `EventEmitter` (the event source), the `Event Loop` (the cycle checking the queue), and `libuv` (non-blocking I/O). **Key point:** this model is inefficient for CPU-intensive tasks - while the thread is busy computing, it can't react to other events, so that work should be offloaded to Worker Threads.Shown above the full answer for quick recall.Answer (EN)Image## 1. What the event model is, in general > The **event model** is an architectural principle > where a program **doesn't run tasks sequentially**, but **reacts to events** happening in the external or internal environment. Instead of "do this, then that," the principle is "**when event X happens, do Y**". ### An example to build intuition Instead of blocking code: ```javascript const data = fs.readFileSync('file.txt'); console.log(data); ``` in the event model: ```javascript fs.readFile('file.txt', (err, data) => { console.log(data); }); ``` Here: - Node.js *doesn't wait* for the file to be read; - it *reacts* once the system **emits the event** "reading finished". ## 2. How this is built inside Node.js Node.js's event model rests on two key mechanisms: 1. **EventEmitter**, an object that emits events and lets others subscribe to them. 2. **The Event Loop**, a mechanism that constantly checks whether there are events or callbacks ready to run. ### A simplified Event Loop cycle ```javascript while (the program is running) { wait for events (I/O, timers, network) if an event happened: call its handler (callback) } ``` ## 3. The pieces of the event model | Component | Description | |---|---| | **EventEmitter** | The "event source". Emits events and calls listeners (`on`). | | **Event Loop** | The cycle that watches for events and calls callbacks. | | **Callback Queue** | The queue of tasks (event handlers) waiting to run. | | **libuv** | The low-level library implementing non-blocking I/O and OS integration. | ## 4. An example event lifecycle ```javascript const fs = require('fs'); fs.readFile('file.txt', (err, data) => { console.log('File read!'); }); ``` What happens "under the hood": 1. Node.js sends the file-read task to **libuv** (the I/O API). 2. libuv runs the operation on a separate **pool thread**. 3. Once the operation finishes, **libuv emits an event**: "result ready". 4. The **Event Loop** picks up that event. 5. The callback from `readFile()` is placed in the **event queue**. 6. The Event Loop calls the callback → "File read!" prints to the console. ## 5. Why the event model is efficient | Advantage | Explanation | |---|---| | Non-blocking I/O | The thread doesn't wait for operations to finish, it handles other events. | | Scalability | One thread can handle thousands of connections. | | Fewer resources | No need to spawn a thread or process per connection. | | Simple reactive logic | Instead of wait loops, subscriptions to events (`on`, `once`). | ## 6. When the event model doesn't fit It's **inefficient for CPU-intensive tasks** (for example, computation, cryptography, video parsing). While a thread is busy computing, it can't react to other events. Solutions: - use **Worker Threads**; - split heavy tasks into **asynchronous pieces**; - offload computation to a separate microservice. ## Summary > **Node.js's event model** is an architecture where program execution is driven by **events and reactions to them**, > rather than sequential code execution. > > It rests on: > > - **EventEmitter** (emitting events), > - **the Event Loop** (processing events), > - **the Callback Queue** (the queue of callbacks). > > Thanks to this, Node.js can efficiently run **non-blocking I/O** > and serve **tens of thousands of connections on a single thread**.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.