Blocking And Non Blocking In Node js: What Does It Mean?

Blocking And Non Blocking In Node js: What Does It Mean?

Are you puzzled by the terms “blocking” and “non-blocking” in Node.js? Understanding these concepts is important for optimizing your Node.js applications. In this blog, we’ll delve into what blocking and non blocking in node js mean. By grasping the differences between these two things, you’ll gain insights into how Node.js handles I/O operations and concurrency. Whether you’re a new or an experienced developer, knowing the mysteries of blocking and non blocking in Node js will enhance your proficiency in building efficient and scalable applications.

In Search For Better Development In Nodejs For Your Digital Technology? Click Here

Blocking and Non Blocking in Node Js

Blocking and Non Blocking in Node Js

A blocking or non-blocking operating model is a process that deals with input/output operations.

Reading from a source or writing to a resource is considering an I/O operation. “I/O” refers to the interaction with the system’s disk and network supported by the libuv module.

On the other hand, the random-access memory (RAM) has the ability to access files from any location in the same amount of time irrespective of their physical location. This memory can be addressed quickly and doesn’t require waiting. So accessing memory is not considered an I/O operation.

The most common tasks carried out by most of the developers is File I/O, this fundamental process has three processes:

  • Open a File.
  • Read its contents and print.
  • Executing something.

What Is Blocking In Nodejs?

What Is Blocking In Nodejs?

A Blocking term is originally originated from the operating system process model. It is actually a multitasking operating system, each labeled process with a state depending on how ready those processes are to be put on the CPU for execution.

When JavaScript execution in the Nodejs process has to wait until a Non-JavaScript operation to complete is called blocking.

A process is labeled as blocked if it is not ready for execution. So it will wait for an event to occur. Each Input/Output event indicates either progress or completion in an I/O operation.

Performing a blocking system call forces the entire process to enter into the blocked state.

Let’s see how this can be done with blocking code in Nodejs:

Here we try to read simple files: hosts and users and printing their contents, meanwhile printing a few welcome messages.

Read More: What are the basic primitives of Nodejs?

Node js i/o Operations Blocking/Synchronous Code:

Contents of the hosts file :

190.158.0.1

173.0.1.1

205.250.255.0

Contents of users file :

paul

smith

dog

Snake
var fs = require('fs');

 var contents = fs.readFileSync('users','utf8');

 console.log(contents);

 console.log("Welcome Node\n");

 var contents = fs.readFileSync('hosts','utf8');

 console.log(contents);

 console.log(“Welcome again!”);

Let’s see the output down here for Blocking/Synchronous code :

paul

smith

dog

Snake
Welcome Node
190.158.0.1

173.0.1.1

205.250.255.0
Welcome again!

By relating to the output code, we may come to know about some major points:

  • It’s a blocking code
  • We see the contents of the user file, the ‘Welcome Node’ strongly supports the above point.
  • Same happens with the next file.

Read More: How to Update Image in PHP Without Changing the Uploaded Image Fields?

What Is Non Blocking in Nodejs?

What Is Non Blocking in Nodejs

A non-blocking operation in Node js does not wait for I/O to complete. Whenever a blocking operation happens in the process, all other operations will put on hold at the operating system level. Performing such a non-blocking I/O operation, the process continues to run in the non-blocking mechanism. 

A non-blocking call initiates the operation and leaves it for OS to complete returning immediately without any results. 

Let’s see how this can be done with non-blocking code in Nodejs:

Here we try to read simple files: hosts and users and printing their contents, meanwhile printing a few welcome messages.

Read More: What is the callback function in Node.js?

Node js Non Blocking/Asynchronous Code:

Contents of the host’s file :

190.158.0.1

173.0.1.1

205.250.255.0

Contents of users file :

paul

smith

dog

Snake
var fs = require('fs');

 

var contents = fs.readFile('./users','utf8', function(err,contents){

   console.log(contents);

});

console.log("Welcome Node\n");

 

 var contents = fs.readFile('./hosts','utf8', function(err,contents){

   console.log(contents);

});

console/log(“Welcome again”);

Let’s see the output down here for Non-blocking or Asynchronous code :

Welcome Node

Welcome again!

paul

smith

dog

Snake

190.158.0.1

173.0.1.1

205.250.255.0

By comparing with the previous sync code output, we can conclude the below points with async code :

  • The above-executed codes show a non-blocking output

Conclusion

We hope that this blog blocking and non blocking in node js helped you in a good way. Still, confused with the process? Please contact our Nodejs experts for more details regarding the development process.

Launching Headless Ecommerce in Node.js, Powered by Microservices. Click Here to Inquire for Your Business.

Blocking and Non Blocking In Node js FAQs

1. What is blocking vs non-blocking in Node.js?

In Node.js, blocking refers to synchronous operations where the program waits for an I/O operation to complete before moving on. Node js non blocking, on the other hand, involves asynchronous operations where the program continues to execute without waiting for the I/O operation to finish.

2. What is the difference between blocking and non-blocking processes?

Blocking processes halt the execution of code until a task completes, causing delays. Non-blocking processes allow code to continue running while waiting for tasks to complete, improving efficiency.

3. What does non-blocking mean?

Non-blocking refers to asynchronous operations in which code execution continues without waiting for I/O operations to complete. This approach enhances the responsiveness and scalability of applications, as it allows for concurrent processing of multiple tasks.

4. What are I/O operations in Node.js?

In Node.js, I/O (Input/Output) operations involve reading from or writing to external sources like files, databases, or network sockets. Asynchronous I/O operations are commonly used to ensure that the application remains responsive during data retrieval or transmission.

5. What is non-blocking in Node.js?

Node js non blocking refers to its asynchronous nature, where I/O operations do not block the execution of code. Instead, callbacks or promises are employed to handle the completion of tasks, allowing the program to continue executing other operations in the meantime. This enhances performance and responsiveness.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *