WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Basic · question 9 of 100

What are event emitters in Node.js and how do they work?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

Event Emitters are core components of the Node.js Event-Driven Architecture, which allows objects called "emitters" to emit named events that cause "listener" functions to be called asynchronously. They provide an implementation of the Observer Pattern, a time-tested software design pattern that promotes loose coupling between components and makes it easy to create scalable and maintainable applications.

In Node.js, the ’events’ module provides the EventEmitter class, which can be used to create custom event emitters by extending it. Let’s discuss how event emitters work in Node.js with some examples:

1. Importing the ‘events‘ module:

const EventEmitter = require('events');

2. Creating a custom event emitter class:

class MyEmitter extends EventEmitter {}

3. Instantiating the custom event emitter:

const myEmitter = new MyEmitter();

4. Adding an event listener for a custom event:

myEmitter.on('myEvent', (data) => {
  console.log('myEvent triggered:', data);
});

5. Emitting the custom event:

myEmitter.emit('myEvent', 'Hello, Node.js!');

When the ‘myEvent‘ is emitted by calling the ‘emit()‘ method, the specified listener function will be executed, and it will log the message to the console.

Event Emitters work through the following key methods:

- `on(eventName, listener)`: Adds a listener function for the given event name.
- `once(eventName, listener)`: Adds a one-time listener function for the given event name.
- `removeListener(eventName, listener)`: Removes the specified listener function for the given event name.
- `removeAllListeners([eventName])`: Removes all listeners or those of the specified event name.
- `listeners(eventName)`: Returns an array of the listeners for the specified event name.
- `emit(eventName[, ...args])`: Emits the event with the given event name, passing the optional arguments to the listener functions.

It’s essential to understand that events in Node.js are processed asynchronously using the event loop, which ensures that the main thread remains unblocked and I/O operations can continue while waiting for events. This feature is crucial for building high-performance and scalable applications.

In summary, Event Emitters in Node.js play a vital role in providing an efficient way to handle events and manage application workflows. They promote modularity, decoupling, and scalability in Node.js applications by implementing an event-driven architecture based on the Observer Pattern.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics