0
0
Node.jsframework~10 mins

Custom event emitter classes in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom event emitter classes
Create EventEmitter Class
Add event listener method
Add event emit method
Register listener for event
Emit event with data
Listener runs with data
End
This flow shows how a custom event emitter class is created, listeners are added, and events are emitted to trigger those listeners.
Execution Sample
Node.js
class MyEmitter {
  constructor() {
    this.events = {};
  }
  on(event, listener) {
    (this.events[event] ??= []).push(listener);
  }
  emit(event, data) {
    (this.events[event] ?? []).forEach(fn => fn(data));
  }
}

const emitter = new MyEmitter();
emitter.on('greet', msg => console.log(msg));
emitter.emit('greet', 'Hello!');
Defines a simple event emitter class, adds a listener for 'greet', then emits 'greet' with a message.
Execution Table
StepActionState ChangeOutput
1Create MyEmitter instanceevents = {}
2Call on('greet', listener)events = { greet: [listener] }
3Call emit('greet', 'Hello!')Listener functions called with 'Hello!'Console logs: Hello!
4No more listenersNo state change
5End of executionFinal events = { greet: [listener] }
💡 All listeners for 'greet' have been called, no more events emitted.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
eventsundefined{}{ greet: [listener] }{ greet: [listener] }{ greet: [listener] }
Key Moments - 2 Insights
Why does the events object hold arrays of listeners instead of single functions?
Because multiple listeners can be registered for the same event, the events object stores an array of functions for each event key, as shown in step 2 of the execution_table.
What happens if emit is called for an event with no listeners?
The emit method uses a fallback empty array (step 3), so no functions run and no errors occur, the state remains unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does the events object contain?
A{}
B{"greet": [listener]}
C{"greet": listener}
Dundefined
💡 Hint
Check the 'State Change' column at step 2 in the execution_table.
At which step does the listener function run and output 'Hello!'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table for when console logs happen.
If you call emit with an event that has no listeners, what happens to the events object?
AIt stays unchanged
BIt throws an error
CIt adds a new empty array for that event
DIt deletes all listeners
💡 Hint
Refer to the key_moments explanation about emit behavior with no listeners.
Concept Snapshot
Custom event emitter classes:
- Create a class with an events object to hold listeners.
- Use on(event, listener) to add listeners (stored in arrays).
- Use emit(event, data) to call all listeners for that event.
- If no listeners, emit does nothing safely.
- Allows multiple listeners per event.
Full Transcript
This lesson shows how to build a custom event emitter class in Node.js. We start by creating a class with an empty events object. The on method adds listener functions to an array for a given event name. The emit method calls all listeners registered for that event, passing data to them. We traced the steps: creating the instance, adding a listener for 'greet', then emitting 'greet' with a message. The listener runs and logs the message. We also saw that emitting an event with no listeners does nothing and does not cause errors. This pattern lets you build flexible event-driven code.