How Node.js Works behind the Scene with Live Practical Examples!

Thapa Technical
63 followers
Dec 14, 2024
Hello what's up guys, today in this video
we will see what's behind the scene, yes guy,
today's video is going to be very important because
today finally you are
going to know
how to do that work behind the scene. Okay, so guys,
first of all we need to understand the
differences between synchronous vs
asynchronous, so here you
can see very clearly that I have just changed from simple to
Keep only two definitions so that your doubt
becomes clear and already we have seen this
if you remember right so once we have this
synchronous code over here and the other one is
the synchronous then what is this synchronous
and then asynchronous so you can see. Yes,
any task that is executed sequentially is
fine, any task that
finishes the first one and then
finishes the second one. Then the third one, then the
fourth one is happening sequence wise
here so they are known as synchronous task also
they are known as blocking task also
okay for example like we have a math
operations okay if you pass any statement
Or looping through arrays etc.
Okay, so that is your synchronous code, you
understood, many like, until the first one is
completed, the second one will
not start. The first one will not start until the second one is completed, the
third one will not start, so each
one has to wait till the end, okay, so here it
is, guys, this task blocks the next one,
I will just give you an example, don't
worry but this
is an example. Second What is a Synchronous
Task Tasks Don't Block Executions It
simply means they are known as the non
blocking task for example when you
Reading a file or when you are
making a network request and setting a
time out so basically this is an asynchronous
task it is possible because of we use a
call back promise and async await and
already we have seen this so if you Want
to Know More About Synchronous and Asynchronous
and How They Works Okay so I
would like to tell when we were working with FS module
So here you can see here we have
this FS module So here you
can see what I wrote curd operations
OK I wrote what curd operations and
file renaming using synchronous version and then I have a I
mean made a video
regarding asynchronous Also,
how can you perform the same curd operation
but in an asynchronous way, so these two videos are
very important please check
out. Now I will highlight only these and
tell you here and
further there are more important things which I
am going to tell you, so for example
here you can see this is the code like we are
requiring a FS module and then We
have this one simple kasol dlog and even this
kasol dlog is also known as your synchronous
task and it is also known as your blocking
task ok and Then again we have this
particular code which is your it's not a
synchronous it's as I mean it's not a sync it's
a sync yes so this is a synchronous
task of yours it means it will block your code
so I control a save
let's Run this code ok here I go and
run so you can see what I am
seeing is start right here you
can see very clearly it is a start and
then I am Getting my whatever is the console dialog by
reading the file because this is the data if
I show you so here we have this
fact t once inside that there is this content so
from this you have come to know one thing that
it was both of the Your blocking task,
this means that
it will not run until it is completed, right, so now
I do this,
above, okay, I have done this, now let's
see what happens, okay, now if I
go here and enter. hits I see now
that part has changed, some time
back I was getting the start first, now the start
is coming to me later because first the
task of reading the file will be
performed and till the time it is not finished
you will have to This is not going to be visible So this is known
as your synchronous task Also known as your
blocking task which will block until the
first one completes The second one is not going to run
Now check it out that now how we know the asynchronous
task So here we
have an asynchronous
task, okay, so let me comment,
so we have already seen
what I showed you just a little while ago, you know the
sync version of the FS module, so
one of the codes is this how. To read a file and then this
is this one is how to read a file
using asynchronous way simple no big deal
ok then definitely check out the video
if you are not a Remembering
what is happening here, it is okay, so again I
run it here and hit enter,
okay,
so now you can see very clearly here
that the data is coming to me, it is coming evenly, okay
but one thing. I do it right now,
see, this was at the last, hence no
problem, but if
I put this part below it, then it is
fine in this way, see, because
according to the rule, this will go first,
after that this one. That one should run then this one should run
you are feeling right if it
was synchronous but in between these two I have
added an asynchronous task so now I have to
see that first it will run no doubt
but after that it will run Or it will
run console dot log. Do you understand
what is the work of synchronous? Brother, if it is
taking some time, then whatever task is behind it,
if it takes less time, then
run it first. I am mine. when time
I will come, I will leave. This fund of Synchronous
is simple best. If it takes me one hour of Synchronous, then
everyone will wait for one hour, even if there are
tasks behind me which hardly
take 5 seconds. Let's check it out. What is it?
I am here. I also make this a
clear run so you can see
very clearly and this is the beauty of
your asynchronous task that this
is running at the last right whereas I have run this
at second number. It was written and
look at the start, it got run before that, so this is how it
works synchronously, it
never stops anyone, now it does one thing at a time,
now I take it to the top, okay,
where first it is in its place. Then we have
this particular a synchronous right at the end
so let's check it out now what
happens so guy now it's your chance you guess
what will happen like first of all I will get start
this code will run or This code
will run, basically tell me the sequence, will it
be 1 2 3 4 or 1 2 4 3 or 1
2 3 4, in which way it is going to run. Let me
know in the comment section and for now, just
pause and try. Do it okay I
hope you have tried so let's check it
out what happens is I run here
and boom guys see now you can see very
clear and that This is how it works Start
came first No Doubt and Aana was to come, after that
this file of yours which we read in a synchronous manner
No Doubt and Aana was also to come but
in the third part ours was this This is basically this
particular code of asynchronous but this is the
Beauty said, brother, it is taking some time,
you can run whatever code is below me, it is
so Ts and here you can see thumbs up
and here you can see thumbs down.
Okay, so now let's check it more and I
think this will be one of the best PPT and one of the
important PPT for new
developers because till now this
doubt is not clear in your mind that
how is it doing asynchronous. Everybody is
like there is only a single thread in the nut which is
fine so you can see here we have
this v8 engine so guys what is its main
function what is its main function To convert
your JavaScript code into machine code for
high performance, simple, you have to say itna
and it handles the synchronous task, yes
guys, this is the v8 engine, it handles
your synchronous task, for example, a
basic computation, you say, or variable
assignment, you say. Take or any such
execution which is happening line by line
then v8 engine handles your synchronous task
but here is The main library here
is the main package because of which note just can
handle a synchronous operation and that is
your leave Yvi library
so you can see very clearly what this
is is a C library
it means that when you You work on React,
so React is not a framework, it is a
library and whose library is it? React is
a JavaScript library, in the same
way it is also Livu which is similar.
Language we have given C as c+, if we read it in this way
then C library is what I mean is C's
library is leave UV that provides note
that with cross platform support for
asynchronous operations and now you understand that as soon as we
say asynchronous, we have a file system to
read. There is networking and much more and
the best thing is guys it leaves UV
right it includes your even thread it is very
important And your thread pool enabling
non blocking task like file reading
networking and timer this is it so guys
nut in which asynchronous task is possible
because of this particular library and
this is your event loop right here
you can see this is your Event loop
and thread pool is being provided
and because of this only this
asynchronous task of yours is possible in your existing
application so I hope. Guys now you
know so we have a v8 for your
synchronous task and then we have this leave
uv for your simple asynchronous task okay
so for synchronous this is for asynchronous
we have this particular library
of C then now this one Of the important is
and and in tomorrow's video I will
come to another and in depth a PPT like final on how not
just work but this It is important, so
see, now you can see very clearly, we
have one particular main thread, we have a main thread,
right where you can see,
we have, just the first task of this main
thread is to initiate, that is, the program and its
simple. It means that this is the part where you are not
just beginning executing the program then we have
this top label code top level code,
you understand like this, any such code which is
Outside your function like
declaring a variable or running a basic command,
so that is,
any such library or
any such file in which your program
or your application is dependent,
you come to this particular part. When it
loads, then you understand
how one by one step by step and the
work is being done here. Pay and then finally we have this
register events call back. So you
understand that its work is to Set a
Function that Will Run When Certain Event
Occurs Like Reading a File or Handling a
Request Alright guys now finally we have come to
our main mechanism note whose
end is called your event
loop even in JavaScript also we had read this event loop
right so Its main job is to keep our
program running successfully
and handle whatever task is assigned to it.
Now we have something.
Called off loading and then we have something
called thread pool now here this is the thread
pool by default it has four threads first
second third and then fourth so by default
it has four threads which is who is
providing it note that obviously your This leave
UV library is providing,
now let me tell you what happens, see and
what is this uploading, I will also
tell you now, so the first thing that
happens is that Whatever lightweight task
or synchronous task you have, it will be
handled by your main thread. For
example, understand that your v8 engine will
handle it, but when it comes to
your heavy weight like your heavy tasks like
system operation. I mean, call it file system operations
or encryption part
or compression, all these
things which our main thread is not
able to do properly.
This is also a heavy weight task, you
assign it to our thread pool, now it will be
here that one by one it will
handle those tasks in the thread pool and
it will make sure that your system is running
properly in the background. Keep going
and end which enables your asynchronous
operation. Do you understand guys, what is happening
here, all the small things will be
handled by the main thread and all the heavy weight
ones will be handled by you. The task is fine like file system,
I told you cryptography,
compression and all two things happen is that
it is uploaded from the main thread,
on thread pull and here what is done to them one by one,
guys it
is performed and Once it's done it is sent back to our event
loop you know and
there it gets executed so this
is how your note just works behind the scene
where we have a
google2 loop That's also a part of your leave UV.
Do it's leave in your main thread
and thread pull is a little separate so ya
guys this is it, that's all for this video today
and I really hope you liked the video very
much. And finally these
three slides would have given you an idea of
what is sink versus a sink and then you are
this particular note whose elements are two to
two and then at the end we have this nut.
How is the program basically starting
and how is it executing all the work one by
one so ya guys this
is it, that's all for this video today and I really would have
liked the video very much
one more time this is synchronous How
can you do a card operation in a synchronous mode?
Please check it out. So
guys, if you like the video then please like it.
Share it with your friends and if you have
any doubt. Let me know in the comment
section so guys our next target is 8
lakh subscribers if you haven't subscribed yet then
please do so I will meet
you in the next video till then guys
thank you so much take care
[music]
