Node.JS Big Picture.
!WeeW That was complex enough
Let’s Break It Down
Why Libuv library
The libuv library is written in c and c++ which expose ability to manipulate the low level computer functionality such as networking, database queries, Child processes, thread pooling etc.
HOW DOES NODE ACHIEVE THIS ?
Bindings basically are libraries that “bind” two different programming languages so that code written in one language can be used in code written in another library.
HOW DOES NODEJS PROGRAM RUNS
The nodejs program runs on a single thread and has an event loop which prevents asynchronous code from blocking the thread execution.
Before we proceed we define some terms
A process is an instance of a computer program running. e.g the instance of your microsoft word app running on your PC is a process
This is the basic unit of work in a process.
The thread pool consist of a group of threads awaiting execution.
This is an action by the OS to deteermine which thread in the thread pool to process at a specific time depending on the available resouces.
The nodejs runs the program content on a single thread, at the heart of this thread is the event loop which handles asynchronous code in the program. node pushes these async code to the event loop and thus prevent blockage of the thread.
All asynchronous task are grouped into the following
These includes all pending
These includes long running operations such as reading from files on the hard drives
PENDING OS OPERATIONS
These includes operations that are handled by the OS not CPU intensive e.g network calls, server listening on a PORT
The Event loop works by performing tick, the following below occours on every tick on event loop.
- check pending timers and see if any of thier timeouts have expired, then calls relevant callbacks
- check PendingOSTask or pending operations and calls relevant callbacks
- Pends execution temporarily and waits for new events to occour, i.e. whenever a new pendingTimer, pending Operations or pendingOsOperations is completed.
- Look at the pendingTimers and call any functions with setImmediate
- Handle close events.
After all the pending tasks in the event loop have been executed the node.js program exits.