This tutorial aims to provide a comprehensive understanding of Event Emitters in Node.js and their practical usage. We will be creating custom events and handling them effectively using the EventEmitter class.
By the end of this tutorial, you will be able to:
Prerequisites
Event Emitters in Node.js facilitate the handling of events. An 'event' can be anything that happens in our application, for example, a system event or a user action.
EventEmitter Class
In Node.js, the EventEmitter is a class that we get from the 'events' module. It provides several methods, for instance, emit()
and on()
.
emit(eventName, [arg1], [arg2], [...])
: This method is used to trigger an event.on(eventName, listener)
: This method is used to add a listener function to an event.The listener function gets called whenever the event with the specified event name gets fired.
Example 1: Creating an Event Emitter
// Import the EventEmitter class from the events module
const EventEmitter = require('events');
// Create an instance of the EventEmitter class
const myEventEmitter = new EventEmitter();
// Add a listener for 'event1'
myEventEmitter.on('event1', function(){
console.log('event1 has been fired');
});
// Emit 'event1'
myEventEmitter.emit('event1');
In the above code, we first import the EventEmitter class and then create an instance of it. We then add a listener function for the event named 'event1'. When 'event1' is emitted using the emit()
method, the listener function is called, and 'event1 has been fired' is logged to the console.
Expected output
event1 has been fired
Example 2: Event Emitter with Arguments
const EventEmitter = require('events');
const myEventEmitter = new EventEmitter();
myEventEmitter.on('event2', function(arg1, arg2){
console.log(`event2 has been fired with ${arg1} and ${arg2}`);
});
myEventEmitter.emit('event2', 'foo', 'bar');
In this example, we are passing arguments ('foo' and 'bar') to the listener function through the emit()
method.
Expected output
event2 has been fired with foo and bar
In this tutorial, we have learned:
For further learning, you could explore:
removeListener
or removeAllListeners
methodExercise 1
Create an event emitter for an event named 'event3' and pass two numbers as arguments to the listener function which adds them together.
Solution
const EventEmitter = require('events');
const myEventEmitter = new EventEmitter();
myEventEmitter.on('event3', function(a, b){
console.log(`The sum is: ${a + b}`);
});
myEventEmitter.emit('event3', 5, 10);
Exercise 2
Create an event emitter for an event named 'status' that accepts an object as an argument with two properties: 'code' and 'message'. Log these properties in the listener function.
Solution
const EventEmitter = require('events');
const myEventEmitter = new EventEmitter();
myEventEmitter.on('status', function(status){
console.log(`Status Code: ${status.code}, Message: ${status.message}`);
});
myEventEmitter.emit('status', {code: 200, message: 'OK'});
Feel free to play around with the code and experiment with different scenarios to get a better understanding of Event Emitters.