Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Custom Event Emitter Classes in Node.js
📖 Scenario: You are building a simple notification system in Node.js. You want to create your own event emitter class to handle custom events like 'message' and 'error'. This will help you understand how events work behind the scenes.
🎯 Goal: Build a custom event emitter class that can register event listeners and emit events with data. You will create an instance, add listeners, and emit events to see how your class handles them.
📋 What You'll Learn
Create a class called MyEmitter with a constructor that initializes an empty object to store events.
Add a method called on that takes an eventName and a callback function, and stores the callback in the events object.
Add a method called emit that takes an eventName and optional arguments, and calls all callbacks registered for that event with those arguments.
Create an instance of MyEmitter, register at least one listener for the 'message' event, and emit the 'message' event with a string.
💡 Why This Matters
🌍 Real World
Custom event emitters are useful in Node.js to handle asynchronous events like user actions, data loading, or system signals in a clean and organized way.
💼 Career
Understanding how to build and use event emitters is important for backend developers working with Node.js, as it helps in creating scalable and maintainable applications.
Progress0 / 4 steps
1
Create the event storage in the class
Create a class called MyEmitter with a constructor that initializes an empty object called events to store event listeners.
Node.js
Hint
Think of events as a place to keep all your event names and their listeners.
2
Add the on method to register listeners
Add a method called on to the MyEmitter class. It should take eventName and callback parameters. Inside, check if this.events[eventName] exists; if not, set it to an empty array. Then push the callback into this.events[eventName].
Node.js
Hint
This method lets you add functions that run when an event happens.
3
Add the emit method to trigger events
Add a method called emit to the MyEmitter class. It should take eventName and any number of additional arguments using rest syntax. Inside, check if this.events[eventName] exists. If yes, loop over each callback in this.events[eventName] and call it with the additional arguments.
Node.js
Hint
This method runs all functions registered for an event, passing them any data you give.
4
Create an instance, add a listener, and emit an event
Create a variable called emitter and set it to a new instance of MyEmitter. Use emitter.on to add a listener for the 'message' event that takes one argument text and logs it. Then call emitter.emit with 'message' and the string 'Hello, world!'.
Node.js
Hint
This step shows how to use your class to listen and respond to events.
Practice
(1/5)
1. What is the main purpose of creating a custom event emitter class in Node.js?
easy
A. To replace the need for functions and callbacks
B. To allow different parts of your program to communicate by sending and listening to events
C. To speed up the execution of synchronous code
D. To automatically handle HTTP requests
Solution
Step 1: Understand event emitters
Event emitters let parts of a program send signals (events) and others listen and react to them.
Step 2: Purpose of custom event emitter classes
Custom classes extend EventEmitter to organize and manage these events clearly.
Final Answer:
To allow different parts of your program to communicate by sending and listening to events -> Option B
Quick Check:
Event communication = C [OK]
Hint: Event emitters enable communication between code parts [OK]
Common Mistakes:
Thinking event emitters speed up synchronous code
Confusing event emitters with HTTP handling
Believing event emitters replace all functions
2. Which of the following is the correct way to listen for an event named data in a custom event emitter instance myEmitter?
easy
A. myEmitter.on('data', callback)
B. myEmitter.emit('data', callback)
C. myEmitter.listen('data', callback)
D. myEmitter.trigger('data', callback)
Solution
Step 1: Identify the method to listen for events
The on method is used to register a callback for an event.
Step 2: Check other options
emit triggers events, listen and trigger are not valid EventEmitter methods.
Final Answer:
myEmitter.on('data', callback) -> Option A
Quick Check:
Listen with on() = A [OK]
Hint: Use on() to listen, emit() to send events [OK]
Common Mistakes:
Using emit() to listen instead of on()
Using non-existent methods like listen() or trigger()
Confusing event names with method names
3. Consider this code snippet:
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const emitter = new MyEmitter();
emitter.on('greet', name => console.log(`Hello, ${name}!`));
emitter.emit('greet', 'Alice');
What will be printed when this code runs?
medium
A. greet Alice
B. Error: greet event not found
C. Hello, Alice!
D. undefined
Solution
Step 1: Understand event registration
The on method registers a listener for 'greet' that prints a greeting with the name.
Step 2: Understand event emission
The emit method triggers 'greet' with argument 'Alice', so the listener runs and prints the message.
Final Answer:
Hello, Alice! -> Option C
Quick Check:
Emit triggers listener output = B [OK]
Hint: emit() runs on() listeners with given arguments [OK]
Common Mistakes:
Expecting event name or arguments to print directly
Confusing emit() with on()
Assuming error if no listener exists
4. What is wrong with this custom event emitter code?
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const emitter = new MyEmitter();
emitter.emit('start');
emitter.on('start', () => console.log('Started'));
medium
A. The event name 'start' is invalid
B. emit() cannot be called without arguments
C. You must call super() in the constructor of MyEmitter
D. The event listener is registered after the event is emitted, so it won't run
Solution
Step 1: Check event listener registration timing
The listener for 'start' is added after the event is emitted, so it misses the event.
Step 2: Validate other options
emit() can be called without arguments, super() is optional if no constructor, and 'start' is a valid event name.
Final Answer:
The event listener is registered after the event is emitted, so it won't run -> Option D
Quick Check:
Listener after emit = no output = D [OK]
Hint: Register listeners before emitting events [OK]
Common Mistakes:
Calling emit() before on() listener
Thinking emit() needs arguments always
Assuming constructor must call super() if none defined
5. You want to create a custom event emitter class that counts how many times an event named ping is emitted. Which code correctly implements this behavior?
hard
A. class PingCounter extends EventEmitter {
constructor() {
super();
this.count = 0;
this.on('ping', () => this.count++);
}
}
B. class PingCounter extends EventEmitter {
constructor() {
this.count = 0;
this.on('ping', () => this.count++);
super();
}
}
C. class PingCounter extends EventEmitter {
count = 0;
on('ping', () => this.count++);
}
D. class PingCounter extends EventEmitter {
constructor() {
super();
this.count = 0;
this.emit('ping', () => this.count++);
}
}
Solution
Step 1: Proper constructor and super() call
class PingCounter extends EventEmitter {
constructor() {
super();
this.count = 0;
this.on('ping', () => this.count++);
}
} correctly calls super() first in constructor, required before using this.
Step 2: Correct event listener setup
class PingCounter extends EventEmitter {
constructor() {
super();
this.count = 0;
this.on('ping', () => this.count++);
}
} uses this.on('ping', () => this.count++) to increment count on each ping event.
Step 3: Check other options for errors
class PingCounter extends EventEmitter {
constructor() {
this.count = 0;
this.on('ping', () => this.count++);
super();
}
} calls this.on before super(), causing error. class PingCounter extends EventEmitter {
count = 0;
on('ping', () => this.count++);
} has invalid syntax outside constructor. class PingCounter extends EventEmitter {
constructor() {
super();
this.count = 0;
this.emit('ping', () => this.count++);
}
} wrongly uses emit instead of on.
Final Answer:
class PingCounter extends EventEmitter {
constructor() {
super();
this.count = 0;
this.on('ping', () => this.count++);
}
} -> Option A
Quick Check:
super() first, then on() listener = A [OK]
Hint: Always call super() before using this in constructor [OK]