Home >>Nodejs Tutorial >Node.js Events

Node.js Events

Node.js Events

Events and Callbacks concepts are used for providing competition in Node.js applications. As Node.js applications are single threaded, each Node js API is asynchronous. So to maintain competition it uses async function. Node uses pattern for observer. Node thread keeps an event loop and it fires the corresponding event after the completion of any task which signals the function of the event listener to be executed.

Event Driven Programming

Node.js uses programming which is driven by event. It means that as soon as the Node starts its server, it simply initiates its variables, declares functions and then waits for event to happen. It is one of the reasons why Node.js is relatively fast in comparison with other similar technologies.

In the event driven application, there is a main loop that listens for events, and then triggers a callback function if one of those events is detected.

Difference between Events and Callbacks:

Events and callbacks look identical though, however the distinctions are due to the fact that callback functions are called when an asynchronous function returns its result while the pattern of the observer acts as event handling. Once an event is fired the listener feature starts to execute. Node.js has several built-in events accessible via module events and class EventEmitter which is used to connect events and listeners to events.

Bind event and event listener to eventEmitter class:
// Import events module  
var events = require('events');  
// Create an eventEmitter object  
var eventEmitter = new events.EventEmitter();  
Binding event handler to an event:
// Bind event and even handler as follows  
eventEmitter.on('eventName', eventHandler);  
Fire an event with:
// Fire an event   

Node.js Event Example

File: main.js

// Import events module  
var events = require('events');  
// Create an eventEmitter object  
var eventEmitter = new events.EventEmitter();  
// Create an event handler as follows  
var connectHandler = function connected() {  
console.log('connection succesful.');  
// Fire the data_received event   
// Bind the connection event with the handler  
eventEmitter.on('connection', connectHandler);  
// Bind the data_received event with the anonymous function  
eventEmitter.on('data_received', function(){  
console.log('data received succesfully.');  
// Fire the connection event   
console.log("Program Ended.");  

Now open the prompt for the Node.js command and run the code below:

node main.js 

Node.js Events

How Node Applications Work?

Any async function accepts callback as the last parameter in Node Code, and a callback function accepts an error as the first parameter. Let's go look again at the previous example. Creates a text file called input.txt with the below content.

Build a js file with the following code, called main.js-
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
if (err) {
console.log("Program Ended");

Here fs.readFile() is an async function aimed at reading a file. If an error occurs during the reading process, the err object must contain the error, otherwise the contents of the file will be stored in the data. After the read operation is complete, readFile passes error and data to the callback function, which eventually prints the text.