Nisha Patel
When an incoming request arrives at your server, it is fielded by a listening object at the OS level (in the TCP stack). So, at the OS level, that results in the creation of a new socket which represents a bi-directional TCP connection between client and server. That socket is essentially the ID that identifies a particular connection between two endpoints.
Meanwhile, Node.js effectively manages the event loop through its non-blocking, asynchronous architecture. The event loop is a crucial part of Node.js, responsible for handling I/O operations and executing callback functions. Here's an overview of how Node.js achieves this:
Single-threaded and Asynchronous: Node.js is designed to be single-threaded, but it uses an event-driven, non-blocking I/O model. This means that the main thread (event loop) handles multiple operations concurrently without blocking the execution of other tasks.
Event-driven Architecture: Node.js relies on an event-driven architecture where events trigger the execution of specific functions or callbacks. Events can be I/O operations, timers, or user-generated events. When an asynchronous operation is initiated, Node.js registers a callback function and continues with other tasks.
Libuv Library: Node.js uses the Libuv library to implement the event loop. Libuv provides a platform-independent abstraction for asynchronous I/O operations and handles events efficiently. It manages things like file system operations, networking, and timers.
Event Loop Phases:
Timers: Executes scheduled callbacks that are ready to run.
I/O callbacks: Handles I/O-related events like file and network operations.
Idle, prepare: Used internally for system events and resource preparation.
Poll: Retrieves new I/O events from the event queue.
Check: Executes setImmediate() callbacks.
Close callbacks: Executes callbacks registered with close events.
Also, the listening object in the Nodejs process gets notified that there's a new connection on its HTTP server. This goes through the Nodejs event queue where a particular phase of the event queue checks for these incoming notifications and, if found, then calls a Javascript callback listener for that notification. At this point, it's just running normal Javascript within Nodejs.
First question: Is this request for a new connection a new process or a new thread?
The initial part of creating the new connection is done within the OS and it's really up to the OS how it does that. It likely uses some threads within the OS (inside the TCP stack), but no new thread is created for each incoming connection.
Then, the incoming connection is processed by a particular phase of the nodejs event loop which does not use a new thread and does not create a thread for the incoming connection. The incoming connection is just processed by the normal operation of the nodejs event loop and the Javascript callback it calls to process this event from the event loop is just a normal Javascript callback running in the single Javascript thread of the interpreter.
Second question: The event loop is for managing asynchronous tasks, how can it find out which request this performed task is related to?
The network stack in the OS creates a socket object that uniquely identifies a specific connection between two endpoints. The TCP library within nodejs uses that socket object. It gets wrapped into a nodejs socket object which the HTTP library uses. When traffic arrives on that socket or is sent to that socket, it uses the TCP socket object as the identifier so the underlying TCP stack knows which endpoint to send the data to or which endpoint incoming data came from.
How the result of these requests is sent to each of them I think there should be a part to map the ID of each new request with its task in the JavaScript engine
The TCP stack in the OS knows how to use the socket object that it created to know which endpoints that socket belongs to, what state the connection is in, what settings it has, what traffic is buffered for it, etc...
I expect somewhere in the operating system or in javascript
In the native code in the TCP stack in the OS.
Answered by:> Yong Shun
Credit:> StackOverflow
Blog Links:
>Why Swagger UI does not show the Choose a File button?
>How to measure the time queries take to run in NodeJS?
>While npm run dev do nothing and crash in Nodejs?
>How to update a document in MongoDB with NodeJS?
>How can I write a new file in Nodejs?
>How to overwrite the contents of MongoDB via a put request in Nodejs?
>NodeJS Error Solved: Handling Not Sending Error To Frontend
>How to wait until I have my uploaded URL?