Node Js events module has only one class to handle events which is EventEmitter class. It contains all required functions to take care of generating events.
All objects that emit events are instances of the EventEmitter class. You might already know about many Node library events like “on data”, “on end”, “on error”, etc. we can say these Node libraries extend EventEmitter.

EventEmitter Responsibility

EventEmitter class is responsible to generate events. Generating events is also known as Emitting. That’s why this class is named as EventEmitter.

We can understand this Process through the given Diagram

 Example of EventEmitter: 
var EventEmitter = require('events').EventEmitter;
var light = new EventEmitter();
light.on('sprading', function(light_ray) {
    console.log(light_ray);
});
setInterval(function() {
    light.emit('sprading', 'lightray');
}, 2000);

EventEmitter “emit()” function

EventEmitter class has a “emit()” function, which is used to create an Event. It takes one parameter.

eventEmitter.emit(Name Of Event); 

Here instead of ‘Name of event’ we need to pass Event Name to emit() function. As in the following example we have passed ‘appon’ as name of our event.

Consider the following example

var events = require('events');
var eventsEmitter = new events.EventEmitter();
eventsEmitter.emit('appon');

EventEmitter “on()” function

EventEmitter class has a “on()” function, which is used to bind an Event with an Event Handler JavaScript Function. It has two parameters.

eventEmitter.on(Name Event To Bind, EventHandler Function);

Here instead of ‘Name Of Event To Bind’ we will pass our ‘Event Name’  to on() function.
Consider the following example js function event handler

var events = require('events');
var eventsEmitter = new events.EventEmitter();
eventsEmitter.emit('appon', function(data){
    console.log(data);
});
eventsEmitter.emit('appon');

We can also use Plain JavaScript function as Event Handler as shown below

var events = require('events');
var eventsEmitter = new events.EventEmitter();
eventsEmitter.emit('appon',appOnHandler);
eventsEmitter.emit('appon');
function appOnHandler(data){
    console.log(data);
}

Create a text file “Neel.txt” with the following content. We are going to use this file to test Node Js events.

Dear, Neelesh Readers. 
Thank You for your support.

Create a JavaScript file ‘event.js ‘.

var events = require("events");
var fs = require('fs');
var eventsEmitter = new events.EventEmitter(); 
eventsEmitter.on('read',readFileContent);
eventsEmitter.on('display',displayFileContent);
eventsEmitter.on('finished',finished);
eventsEmitter.emit('read','Neel.txt'); 
function readFileContent(fileName){
    console.log("Reading " + fileName + " file started:");
    fs.readFile(fileName, 'utf8', readFile);
} 
function  displayFileContent(data){
    console.log("File Data:");
    console.log(data);
    eventsEmitter.emit('finished');
} 
function finished(){
  console.log("Reading and Printing File content job is done successfully.");
} 
function readFile(err,data,fileName) {
    console.log("Reading " + fileName + " file done successfully.");
    eventsEmitter.emit('display',data);
}

Run this JS file and observe the output at  IDE Console

C:\Users\Magnet Brains\Desktop\node-app>node event4.js
Reading Neel.txt file started:
Reading undefined file done successfully.
File Data:
Dear, Neelesh Readers.
Thank You for your support.
Reading and Printing File content job is done successfully.

By observing this output, we are successfully raised some events while reading a file and handle them

Learn More-