Events are actions recognized by a software like opening or closing a file. In a programming language, an events is an action that initiates the execution of another piece of code. Nodejs has an in-built class named EventEmitter in the events module which is used to create ,fire and listen for user-built events. AlThough nodejs is a single threaded application, it supports concurrency with the help of events and callbacks. Using events makes nodejs very fast. Whenever an event gets fired, its listener function starts executing. These Listener functions are called Observers.
- Importing events module using require directive.
- Creating an EventEmitter object.
- Creating an Event Handler.
- Binding an event with the event handler.
- Firing an event.
As already mentioned above events module of Nodejs contains EventEmitter class which is used to handle events. So this module is imported using require directive.
var events = require('events');
An object of EventEmitter class is created which helps us to use the various methods provided by EventEmitter class to work with events.
var event_object = new events.EventEmitter();
Event Handler is a piece of code that is executed in response to an event. Event Handler is basically a function which is invoked when an event is fired.
var eventHandler = function(){
console.log("message is received");
}
on() method of EventEmitter class is used to bind together an event and event handler so that it is invoked when that event is fired.
event_object.on('event_name',eventHandler);
emit() method is used to fire an event.
event_object.emit('event_name');
EventEmitter class provides various in-built methods used to raise and handle custom events. It provides multiple properties like on and emit. on property is used to bind a function with the event and emit is used to fire an event.
An object of EventEmitter class is created and used to invoke methods of EventEmitter class using the following code ->
var event_object = new events.EventEmitter();
Method ⚽ | Description 👇 |
---|---|
addListener(event, listener) | Adds a listener at the end of the listeners array for the specified event. |
on(event, listener) | Adds a listener at the end of the listeners array for the specified event. |
once(event, listener) | Adds a one time listener to the event. This listener is invoked only the next time the event is fired, after which it is removed. |
removeListener(event, listener) | Removes a listener from the listener array for the specified event. |
emit(event, [arg1], [arg2], [...]) | Execute each of the listeners in order with the supplied arguments. |
listeners(event) | Returns an array of listeners for the specified event. |
There are two ways used to raise and bind an event using EventEmitter class in Nodejs.
- Return EventEmitter from a function
- Extend EventEmitter class
In the below code, inside a function we are creating an object of EventEmitter class and then returning it. Then we use the return value of the object to bind an event.
var emitter = require('events').EventEmitter;
function ReturnObject(num){
var event_object = new emitter();
setTimeout( function(){
for(var i = 1; i<= num; i++){
event_object.emit('BeforeProcess',i);
console.log('Processing number is: ' + i);
event_object.emit('AfterProcess', i);
}
}, 1000);
return event_object;
}
var temp = ReturnObject(5);
temp.on('BeforeProcess', function(data){
console.log('Starting process for: ' + data);
});
temp.on('AfterProcess', function(data){
console.log('Completed processing:' + data);
});
The output of the above code is 👀 ->
We can extend the constructor function from EventEmitter class to emit the events. we use util.inherits() method to inherit from EventEmitter class.
var emitter = require('events').EventEmitter;
var util = require('util');
function ReturnObject(num){
var event_object = this;
setTimeout( function(){
for(var i = 1; i<= num; i++){
event_object.emit('BeforeProcess',i);
console.log('Processing number is: ' + i);
event_object.emit('AfterProcess', i);
}
}, 1000);
return this;
}
util.inherits(ReturnObject, emitter);
var temp = new ReturnObject(3);
temp.on('BeforeProcess', function(data){
console.log('Starting process for: ' + data);
});
temp.on('AfterProcess', function(data){
console.log('Completed processing:' + data);
});
The output of the above code is 👀 ->