node js - Node js Event Loop - node - node js tutorial - webnode




Related nodejs article tags - node js - node js tutorial - node js examples

What is Events in Nodejs?

  • Each objects in Node emit an asynchronous event. event is handled by an object EventEmitter.
  • EventEmitter is underneath every object.
learn nodejs - nodejs tutorial - nodejs events - nodejs example programs
learn nodejs - nodejs tutorial - nodejs events - nodejs example programs
Related nodejs article tags - node js - node js tutorial - node js examples

What is Event Loop in Node.js?

  • Node.js is a single-threaded application, but it can support concurrency through the concept of event and callbacks.
  • Every API of Node.js is asynchronous and being single-threaded, they use async function calls to maintain concurrency.
  • Node uses observer pattern.
  • Node thread saves an event loop and whenever a task gets completed, it fires the corresponding event which signals the event-listener function to execute.
 process of node.js event loop

Learn Node js - node js Tutorial - process of node.js event loop - - node - Node js Examples

Event-Driven Programming

  • Node.js uses events heavily and it is also one of the reasons why Node.js is pretty fast compared to other similar technologies.
  • As soon as Node starts its server, it simply initiates its variables, declares functions and then simply waits for the event to occur.
  • In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected.
 event driven application in node.js event loop

Learn Node js - node js Tutorial - event driven application in node.js event loop - - node - Node js Examples

  • Although events look quite similar to callbacks, the difference lies in the fact that callback functions are called when an asynchronous function returns its result, whereas event handling works on the observer pattern.
  • The functions that listen to events act as Observers.
  • Whenever an event gets fired, its listener function starts executing.
  • Node.js has multiple in-built events available through events module and EventEmitter class which are used to bind events and event-listeners as follows
// Import events module
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();
  • Following is the syntax to bind an event handler with an event
// Bind event and event  handler as follows
eventEmitter.on('eventName', eventHandler);
  • We can fire an event programmatically as follows
// Fire an event 
eventEmitter.emit('eventName');

Example

  • Create a js file named main.js with the following code
Related nodejs article tags - node js - node js tutorial - node js examples

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 
   eventEmitter.emit('data_received');
}

// 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 
eventEmitter.emit('connection');

console.log("Program Ended.");
  • To run the above program and check its output
$ node main.js
  • IT should produce the following result
Related nodejs article tags - node js - node js tutorial - node js examples

Output:

connection successful.
data received successfully.
Program Ended.

node js event loop :

learn nodejs - node-js tutorial - node js event loop - nodejs examples -  nodejs programs

learn nodejs - node-js tutorial - node js event loop - nodejs examples - nodejs programs

How Node Applications Work?

 configuration of node.js event loop

Learn Node js - node js Tutorial - configuration of node.js event loop - - node - Node js Examples

  • In Node Application, any async function accepts a callback as the last parameter and a callback function accepts an error as the first parameter.
  • Let's revisit the previous example again.
  • Create a text file named input.txt with the following content.
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!
  • Create a js file named main.js having the following code
Related nodejs article tags - node js - node js tutorial - node js examples

main.js

var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
   if (err){
      console.log(err.stack);
      return;
   }
   console.log(data.toString());
});
console.log("Program Ended");
  • Here fs.readFile() is a async function whose purpose is to read a file.
  • If an error occurs during the read operation, then the err object will contain the corresponding error, else data will contain the contents of the file.
  • readFile passes err and data to the callback function after the read operation is complete, which finally prints the content.
Program Ended
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!

Traditional Web Server Request Vs node js event loop - Web Server handling :

learn nodejs - node-js tutorial - node js -traditional web server client request - nodejs examples -  nodejs programs
learn nodejs - node-js tutorial - node js - event loop - nodejs examples -  nodejs programs

This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js Event Loop