16. Node.js Lessons. Event Loop, libUV Library. Part 1

 

bannerHey, guys! If your habit is to learn something deeply and thoroughly, this article is for you. Here we will try to answer the questions that you will have sooner or later in the process of development. These answers require deep understanding of how Node.js works. For example, here (serverAsync.js see the files of our previous lesson) an asynchronous call is used for reading a file:

But can any operation be asynchronous? Are asynchronous calls that dangerous and what should you do, if there is an asynchronous operation and you cannot avoid it? How can you decrease its adverse effect? What happens to those requests that continue to come while an interpreter is busy? For example, if there are synchronous files (serverSync.js):

and 10 clients have come. Are they being refused in services? Or is there a queue of them? You will learn the answers on these questions in our today’s article.

1

To understand the process better, get familiar with the libUV library. You don’t have to access this library explicitly. It is written in C-language and built into the Node.js server. LibUV is responsible for the two vital things. The first one is cross-platform input/output operations, work with the files and network. Using JavaScript we give a command to Node.js to compute some file or send data via the network. And in order to do it, Node.js uses the libUV within. So, libUV is in charge of cross-platform execution of these functions. It already knows how to work with Windows, Linux, etc.

The second area of responsibility for libUV is the support of a general event loop in Node.js. It turns out that every time we launch a script, it gets launched in this loop’s mode. The loop rotates JavaScript execution, which is conducted with a V8 virtual machine, with waiting for various events: input-output, timer expiration, and libUV is also responsible for these events. This loop will exist until it is possible to have any new events, input/output or timer, which all need to be handled.

Let us look at what’s happening at launching a server of this kind as an example:

First  JavaScript gets active. It connects the modules:

And creates an object:

Gives a handler:

At this point, it doesn’t matter what is inside the function because a handler hasn’t benn activated yet. The last string is a call for the “listen” command.

This is a command of working with network connections.

2

Getting to Node.js the command goes through its C++ code and becomes an internal method call TCPWrap::listen. This internal method, in its turn, calls for libUV, in particular, for its method uv__listen, which executes the whole work. Depending on an operational system, it hangs a connection handler on this port. For instance, the unix system requires a system call “listen.” So, libUV appoints a connection handler for this port.

This handler, or watcher according to the libUV terminology, is internal, so it is not accessible for us. LibUV installed it, and whenever something happens – for example, when new connection gets activated – the watcher will turn on, call the respective libUV methods, Node.js and will likely give us some event – connection, for example. But all these will happen later. At the moment “listen” has only installed a handler. This action’s result gets upwards in the chain. If everything’s ok, it will lead to the listening event in JavaScript. If some mistake occurs, you will see an error.

part2_blogimage1

Materials has taken from following screencast

We are looking forward to meeting you on our website soshace.com

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

How I Built an Admin Dashboard with Python Flask

In this article, I will share the how I built an admin dashboard. I will give a step by step guideline on how I built this application with Python ...

Getting started with Git Hooks using ghooks

Git hooks are configured scripts that are executed at specific moments during git operations. ghooks is a package that configures git hooks so that ...

React Native vs. Flutter: Which One Would Suit You Better?

Unveiling pros and cons of such cross-platform development frameworks as Flutter and React Native. Discover what suits you ...

2 comments

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy