EventEmitter in Node.js
In this article, we shall see how to create and handle the events using the EventEmitter
.
The events are the one that denotes the occurrence of something. Usually, if an user performs any action, then we call it as an event.
To handle the event, we use the event handler. The event handler is generally a function that is to be executed on a particular event.
All objects that emit events are instances of the EventEmitter
. The on()
function in the object of the EventEmitter
class allows us to bind one or more functions to an event name. Usually, the event name will be a camel-cased string.
In order to raise the event, we will use emit()
function in the object of the EventEmitter
.
Example
const events = require("events");
const eventEmitter = new events.EventEmitter();
const onSuccess = () => {
console.log("Successfully completed the event.");
};
eventEmitter.on("onSuccess", onSuccess);
eventEmitter.emit("onSuccess");
In the above example, we have imported the event
module and have created an object eventEmitter
as an instance of EventEmitter
class. Then we have created a function onSuccess()
and add it as the listener function to the event named onSuccess
.
To call the event, we use the emit()
function of eventEmitter
object with the event name onSuccess
.
addListener function
We can also add an event listener using ???addListener' function.
const events = require("events");
const eventEmitter = new events.EventEmitter();
const onError = (message) => {
console.log(`Error: ${message}`);
console.log("");
};
eventEmitter.addListener("onError", onError);
eventEmitter.emit("onError", "Something went wrong.");
In the above example, we have created a function onError()
and add it as a listener function using addListener
to handle onError
event. Next, we call the event using the emit()
function with the event name and message string as the second parameter.
removeListener function
The removeListener
function will remove the listener from the listener array for the event that is specified.
const events = require("events");
const eventEmitter = new events.EventEmitter();
const onError = (message) => {
console.log(`Error: ${message}`);
console.log("");
};
eventEmitter.addListener("onError", onError);
eventEmitter.emit("onError", "Something went wrong.");
eventEmitter.removeListener("onError", onError);
eventEmitter.emit("onError");
console.log("Completed");
Output
Error: Something went wrong.
Completed
In the above code, after removing the onError
event listener it returns empty when we try to invoke the event.
once function
This function will add a one-time listener to the event.
const events = require("events");
const eventEmitter = new events.EventEmitter();
const onError = (message) => {
console.log(`Error: ${message}`);
console.log("");
};
eventEmitter.once("onError", onError);
eventEmitter.emit("onError", "Something went wrong.");
eventEmitter.emit("onError", "Something went wrong.");
console.log("Completed");
Output
Error: Something went wrong.
Completed
In the above code, we have registered the onError()
function as a listener to onError
event using once()
function. Then, when trying to invoke the listener twice it returns result only once.
removeAllListeners function
This will remove all the listener or the specified events. Usually its not a good idea to use this to remove listeners.
Syntax:
removeAllListeners([event]);