Suggest an editImprove this articleRefine the answer for “How do you subscribe to an event?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Through `.on(eventName, listener)` for a permanent subscription or `.once(eventName, listener)` for a one-time one; several handlers on the same event fire in registration order, and unsubscribing is done via `.off()` or `.removeListener()`. **Key point:** this same mechanism works everywhere in Node.js - streams, the HTTP server, sockets, and `process` are all `EventEmitter`s too, using the same `.on()`/`.once()`.Shown above the full answer for quick recall.Answer (EN)Image## 1. Subscribing via `.on(eventName, listener)` `.on()` registers a handler (listener) for a given event. When the event fires via `.emit()`, every handler subscribed through `.on()` gets called. ### Example: ```javascript const EventEmitter = require('events'); const emitter = new EventEmitter(); // Subscribe to the 'message' event emitter.on('message', (data) => { console.log('Message received:', data); }); // Emit the event emitter.emit('message', 'Hello, Node.js!'); ``` Output: ```javascript Message received: Hello, Node.js! ``` ## 2. Subscribing via `.once(eventName, listener)` `.once()` is like `.on()`, but the handler fires **only once**, then removes itself automatically. ### Example: ```javascript emitter.once('connect', () => { console.log('Connection established (once only)'); }); emitter.emit('connect'); // fires emitter.emit('connect'); // won't fire again ``` ## 3. Several subscribers on the same event Several handlers can subscribe to the same event, they fire **in registration order**: ```javascript emitter.on('update', () => console.log('First handler')); emitter.on('update', () => console.log('Second handler')); emitter.emit('update'); ``` Output: ```javascript First handler Second handler ``` ## 4. Removing a subscription To **unsubscribe** from an event, use `.off()` or `.removeListener()`: ```javascript function handler() { console.log('An event happened'); } emitter.on('event', handler); emitter.emit('event'); // fires emitter.off('event', handler); emitter.emit('event'); // doesn't fire ``` ## 5. Where subscribing to events is used in Node.js Many built-in objects are `EventEmitter`s, with the same `.on()` and `.once()` methods: ```javascript const fs = require('fs'); const stream = fs.createReadStream('file.txt'); // Subscribing to system events stream.on('data', (chunk) => console.log('Got a chunk of data')); stream.on('end', () => console.log('Reading finished')); stream.on('error', (err) => console.error('Error:', err)); ``` The same works with: - `http.Server` (`on('request', handler)`) - `net.Socket` (`on('connect', handler)`) - `process` (`on('exit', handler)`) ## Summary > To **subscribe to an event** in Node.js, > use the `.on(eventName, listener)` method of an `EventEmitter` object. > > - `.on()`, a permanent subscription > - `.once()`, a one-time subscription > - `.off()` / `.removeListener()`, unsubscribing > > This mechanism is used **everywhere**: > in streams, HTTP, the filesystem, processes, and custom events.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.