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.


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.


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.


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 To Use Prospector For Python Static Code Analysis

Prospector is a tool that analyzes Python code, outputs information about the errors, potential problems, convention violation, and complexity of the ...


Fluent Validation in ASP.NET MVC

Fluent validation is a validation library for .NET. This will be separated from the business logic. So, it has an advantage over data annotation ...

How to implement WPF Canvas? Explain with an example

Canvas is a lightweight layout of WPF. Canvas is used for 2D graphic design elements but not for UI. You cannot use it for making text-box, checkbox, ...


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