More about Node.js
Node.js Architecture:
As said before, Node.js is majorly dependent upon the Chrome's V8 engine and LIBUV library for its working. Now I guess you know what is V8 Engine and LIBUV library from the Part 1 of this blog. It is to be mentioned that LIBUV is a library in Node.js which is written in C++ and V8 Engine also uses C++ besides using JavaScript. Therefore, Node.js is not just JavaScript as you might have thought.
Now, its time for us to discuss in detail the "Event Loop" and the "Thread Pool" in Node.js.
The Event Loop enables Node.js to perform non-blocking I/O operations in Node.js, even though Node.js is single threaded. The Event Loop is responsible for executing lighter tasks like executing callbacks and network I/O. The Thread Pool in the other hand is responsible for doing heavy tasks line Compression, Cryptography etc.
The Thread Pool:
When we run Node.js in our computer, it's a process which runs in our computer.The process is nothing but an executing program which gets executed in our local computer. When such a process runs, it runs in single thread. Thread is nothing but a sequence of instructions, which are dedicated to perform a given task. We need not to go deep into the thread and process concepts but the necessary parts will be discussed as and when required. But let us remember that as Node.js runs on single thread we must try not to block the thread, no matter how many users or clients access our web application.
When a program is initialized, all the code gets executed apart from the codes , which are inside any callback functions. All the necessary modules are required and the callbacks are registered. Thereafter, the event loop starts running. Thought this event loop will be discussed after this, but for now keep in mind that the "Event-Loop" is the heart of the entire Node.js architecture. It is responsible for major tasks performed in our application.
But sometimes, the tasks are too heavy to be performed by the main thread of Node.js instantly and it then tends to block the execution. The solution to this is the "Thread Pool". It is basically a set of 4 additional threads provided by the LIBUV library. When a heavy task appears, the main thread offloads it to the additional threads to execute it. The heavy tasks like dealing with file system API, compression, cryptography and DNS lookups are offloaded generally. All of these are handled by Node alone. I hope now you have a little idea about the Thread Pool. So, let's talk about the Event Loop.
The Event Loop:
As mentioned in the previous section, the event loop is the heart of the Node.js application. The codes which are in the callback functions runs in the event loop.The event loop is also responsible for handling the offloaded tasks. So, let's see how it works.
Node.js has an event triggered architecture as it uses a lot of these callback functions. If you don't come here from a JavaScript background then here is the definition of callback function. A callback function gets executed as a work gets finished. Soon after a work is done, an event is emitted. The event listener listens to those events and accordingly trigger their respective callback functions. This is how Node.js remains asynchronous. If you want to know more, you can refer to the Node.js documentation at https://nodejs.org/en/docs/
Hope you like the More about Node.js. To learn more follow the next blogs.
Visit : How to download Node.js in windows ?
I have read your blog post on. I am very much pleased with the contents you have mentioned.
ReplyDeleteNode JS Online training
Node JS training in Hyderabad
great tips for aws we at SynergisticIT offer the best aws architect certification training in california
ReplyDeleteNode.Js development has become very popular over the last four years and continues to stand the competition in 2021, making startups worldwide choose it over other available options.
ReplyDeleteDark Bears is the top Mobile App Development Company in USA.
Hire node js experts from Dark Bears and infuse Nodejs in your business better experience.
I need to thank you for this very good read and i have bookmarked to check out new things from your post. Thank you very much for sharing such a useful article and will definitely saved and revisit your site.
ReplyDeleteHire Node.Js App Developers
ReplyDeleteIncredible data in this Post. Keep it up. Much obliged for sharing Love to understand it, Waiting For More new Update and I Already Read your Recent Post its Great Thanks,
6 Reasons You Need An Android App For Your Business
Very informative post....!
ReplyDeleteMore about Node.js
Good for students & learners.....
Well, we are here to share our Microsoft Web Development
microsoft application development
Thanks for sharing informative article with us. You can contact offshore software development company from here.
ReplyDelete