What Is Event Loop In Nodejs

Posted By : Parveen Kumar Yadav | 27-Dec-2017

You have always listened that nodejs uses Single Threaded Event Loop Model to handle multiple requests. But how nodejs can handle multiple requests if it uses single thread model.
Like we all know most of the web application frameworks uses Multithreading models like Java, Spring Mvc,.NET, Ajax etc.
So main question arises in mind is how nodejs handle multiple requests concurrently.

Here Event loop comes in the picture.

 

To understand this in a simple way then you can understand like there are major 4 components:


1)Call Stack
2)Web-API
3)Task Queue
4)Event Loop

 

--

In the above snapshot as you see It works as follows:-

 

First of all, Hi comes in the call stack and console the string.
After that setTimeout comes in the callStack and because its related to web-app its response takes time so it will go to Web-API and after 5 seconds its come into task-queue.
Now here Event loop is checking that weather the call stack is empty or not.
If call stack is empty then it pushes the task from the task queue to call stack (This is how it works)
 
So Event loop is only responsible for shifting or pushing the task from task queue to call stack.
In this way, nodejs handle multiple concurrent request simultaneously.
 
Implicitly EventLoop in code level is done by:-
1)LibEv :- is the event loop which actually runs internally in node.js to perform simple event loop operations.
2)LibEio :- is a library to perform input-output asynchronously. It handles the file descriptors, data handler, sockets etc.
3)LibUv :- is an abstraction layer on the top of libeio , libev, c-ares ( for DNS ) and iocp (for windows asyncronous-io). LibUv performs, maintains and manages all the io and events in the event pool.
Note:- You can check further with you tube video by philip Roberts
 
Link is:-
https://www.youtube.com/watch?v=8aGhZQkoFbQ&t=782s
 
Hope this will help.
 
Queries are welcome.
Thanks

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..