Skip to main content

Command Palette

Search for a command to run...

Node Js Working.

Understanding the Event-Driven Architecture of NodeJS.

Updated
2 min read
Node Js Working.
A

I am a Web Developer and an open-source enthusiast.

Introduction :

Node.js is a powerful runtime environment that utilizes an event-driven, non-blocking I/O model to build scalable and efficient applications. In this article, we will delve into how Node.js works, exploring its event-driven architecture, event queue, event loop, and handling of blocking and non-blocking events.

Event Queue and Event Loop

When a client sends a request to a Node.js server, the request enters the event queue. The event loop, a key component of Node.js, continuously monitors the event queue for incoming events. It follows a first-come, first-served approach, ensuring that events are processed in the order they were received.

Node JS

Blocking vs Non-Blocking Events

NodeJs distinguishes between non-blocking and blocking events. Non-blocking events can be executed asynchronously without blocking the execution of subsequent code. On the other hand, blocking events require additional resources and may cause delays. Node.js efficiently handles both types of events.

How Non-Blocking Events Execute?

For non-blocking events, Node.js executes them in an event-driven manner. The event loop constantly checks for completed non-blocking events, and once an event is finished, its corresponding callback function is triggered, and the output is returned.

How Node.Js handles Blocking Events?

When a blocking event is encountered, Node.js offloads the operation to a thread from the thread pool, also known as a worker. The worker is responsible for executing the blocking operation. If all workers are occupied, the event has to wait until a worker becomes available. This approach prevents blocking events from causing delays or blocking the execution of subsequent events.

Note:

Thread Pool can have a maximum of 10 threads and can only assign 10 threads at a time.

Uses of Non-blocking events

Wonder why we write async await when making an API request.
The async/await is an example of using non-blocking events in NodeJs.
If you don't use async/await and make synchronous API requests using libraries like axios the requests will block the execution of your program until each request is completed. This approach is not recommended, especially in Node.js, because it can lead to poor performance and inefficient resource utilization, as your program will wait for each request to finish before moving on to the next task. This goes against the non-blocking, event-driven nature of Node.js.

const axios = require('axios');

// API endpoint URL
const apiUrl = 'https://jsonplaceholder.typicode.com/posts/1';

// Making an API call with async/await
async function fetchData() {
  try {
    const response = await axios.get(apiUrl);
    console.log('API Response:', response.data);
  } catch (error) {
    console.error('Error making API call:', error.message);
  }
}

fetchData();

Thanks a lot for reading till the end.
Hope you find it useful.

Github: https://github.com/aryan-bhokare
LinkedIn: https://www.linkedin.com/in/aryan-b-3803751a7
Twitter: https://twitter.com/aryan_okidoki