Node.js is an Event driven based platform. This means that everything that happens in Node is the reaction to an event.Event driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads .In any 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 Loop is a single threaded and semi-infinite loop.
- The reason why this is called a semi-infinite loop is because this actually quits at some point when there is no more work to be done.
- If there is work and there is no timer is set for that particular event than loop continues until work is not completed.
- Event loop executes the callback and provide appropriate results. Since, it’s in a loop.. The process simply keeps going.
How does Event loop work in Node js
- The event loop simply iterate over the event queue which is basically a list of events and callbacks of completed operations.
- Node starts the event loop by checking for any expired timers in the timers queue, and go through each queue in each step while maintaining a reference counter of total items to be processed.
- After processing the close handlers queue, if there are no items to be processed in any queue, the loop will exit.
- Processing of each queue in the event loop can be considered as a phase of the event loop.
Libuv is the library that provides the event loop to Node.js.Libuv is cross-platform support library which was originally written for NodeJS.
Event loop’s order of operations :
Note :- Here Each box Represents “Phase” of event Loops
Phases Overview:
- timers: A timer specifies the threshold after which a provided callback may be executed rather than the exact time a person wants it to be executed.
- I/O callbacks: this phase executes almost all callbacks with the exception of close callbacks, the ones scheduled by timers, and setImmediate().
- idle, prepare: this phase is used only for ideal process and prepare for next.
- poll: this phase retrieve new I/O events.
- check:This phase allows a person to execute callbacks immediately after the poll phase has completed.
- close callbacks:If a socket or handle is closed suddenly and unexpectedly.the ‘close’ event will be emitted in this phase.
Between each run of the event loop, Node.js checks if it is waiting for any I/O operation or timers and shuts down if there are not any.
Example Program to monitor the change in a Text File
var fs = require('fs');
fs.watch('target.txt',function(event,filename){
setTimeout(function(){
console.log("Event:"+event + 'For File:' +filename);
},2000);
});
console.log("Now Watching for Change in target file ");
Run Command
C:\Users\Magnet Brains\Desktop\Desktop\my_app>node events1.js
Output:
now watching for change in target file Event:changeFor File:target.txt Event:changeFor File:target.txt
Code Explanation
- In the Above Example we require fs module and create a text file named as “target.txt” ,then we use watch() function to watch the changes in the target.text file.
- When we make some changes in the target text file then some event occurs and file changes seen in command line interface.
- This process repeats and creates a event loop.
- The event loop simply iterate over the event queue which is basically a list of events and callbacks of completed operations.
Learn More-