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

About the author

Stay Informed

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

Stay Informed

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

Related articles

The Path of the Self-Taught Programmer: Avoiding Common Problems

In this article, we’ll explore how a self-taught programmer comes to be: which education opportunities they can utilize, which problems they may>>>


Introduction to GitHub Desktop: A GUI Enhancement to a CLI Approach

In this article, we'll explore various use cases of GitHub Desktop -- GUI software designed to ehance your git workflow. Does it hold up against CLI>>>


Python Array Explained and Visualized

In this tutorial, we’ll delve into how Python arrays work, what their limitations are, and how you can use them to maximize their>>>



Ivan Rastvorov November 28, 2018 at 1:34 pm

опечатка в Async методе. ReadFileSync

Ivan Rastvorov November 28, 2018 at 3:34 pm

Согласно документации написание метода с маленькой буквы

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