What is Node JS and its architecture?

Why NodeJS matter? 

It raises the question and eyebrows of everyone around the corners of the Software Industry lately. It’s the new cool lingo you want to know and why not, since NodeJS has gotten the maximum number of downloads at Github and it has its own repository NPM.

So what is NodeJS?

As per the official description on the Node JS website

Node.js is an open-source, a cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.

But I would to simply it for all our readers scratching their heads compiling all these jargons. Node JS is basically a system software that provides a platform for all application programs (JavaScript Code) to run — Just like Microsoft Windows for VLC media player.

Really is it this simple? Yes, it is and JavaScript is the language that runs on it. NodeJS is an effective Service Side Implementation of JavaScript.

But I don’t want another new technology since there are already enough — Java, DotNet, Python, PHP to name a few.

Yes, we agree but every new technology bridges the gap which its peers couldn’t yet. NodeJS not only helped in the optimization of resources but helped in executing operations asynchronously.

It challenges the potential of the machine and converts the Machine Power into Horse Power.

Sounds Exciting Right? Yes, it is the more you get to know about it.

What are NodeJS Applications?

  • Chat Servers
  • Backend Servers
  • Program Processing Softwares
  • Networking Applications

Synchronous Requests vs Asynchronous Requests

A request is called to be synchronous if it doesn’t have to wait for resources or is dependent on another operation to finish its execution.

Examples of Synchronous Tasks:

1. Addition of 2 number

2. Display a result on the screen

On the other hand, a request is called to be asynchronous if it has a dependency on other operations, tasks or resources over which its execution is dependent.

Examples of Asynchronous Tasks:

1. Pulling data from Database

2. To read a file from Server.

NodeJS Architecture 

Node JS applications use the “Single Threaded Event Loop Model” architecture to handle multiple concurrent clients. I would like to explain the architecture of NodeJS with the help of the diagram inline below :

Basically, NodeJS architecture contains 3 layers:

1. Request Queue

2. Single-Threaded Event Loop

3. Callback Operations

Let me help you understand every layer one by one in the simplest way possible. For the same let’s consider a request statement as quoted below.

Rajeev’s mom asked him to wash clothes and recharge the set-top box.

Request Queue

Here if we try to decode the above-quoted statement then we can decipher 2 requests/tasks for Rajeev:

1. Rajeev will put clothes in the washing machine.

2. Rajeev needs to recharge the expired set-top box TV connection.

Once NodeJS receives the quoted request, it will generate 2 requests and put them in a request queue internally and will wait for the Event Loop to process them.

Single-Threaded Event Loop

From Request Queue, Event Loop will pick up the first request and put that request into operation. Event Loop will check whether this request is Synchronous or Asynchronous.

Request 1 has a dependency on the washing machine which makes it an asynchronous request. So, till the time washing machine is not done washing the clothes, Rajeev has 2 choices to make a decision from :

1. Either he has a choice to wait for 20 minutes and sit idle.

2. Or he can in the meantime recharge the set-top box.

If Rajeev, decides to use his time wisely then he will work on executing the second task instead of sitting idle. So he will let the washing machine complete the task and would like to be notified about its completion in the form of a Callback or here in the form of an Alarm from the Washing Machine.

Feeling connected with NodeJS now? That’s Great !!! Gladly you didn’t notice but we have reached our final layer.

Callback Operations

Are you now scared of Callbacks and Promises? Okay, let me try to explain them.

Callbacks 

A callback function is a function that is passed as an argument to another function. Callback functions are a technique that’s possible in JavaScript because of the fact that functions are objects.

As per our above Request 1, our alarm served as a callback for Rajeev.  Got it right?

Promises

Promises; just like in real life is a token of appreciation that will be fulfilled once a particular objective is achieved.

A mother promises her child to buy her child chocolate if she completes her homework.

Try to understand Request 1 and you’ll find that the washing machine is internally programmed that it will ring the alarm once it completes the task of washing the clothes. So the Washing machine promised Rajeev that it will call back once the task is completed.

Summary :

Get yourself familiar with callbacks and promises. Understand them and use them. Connect NodeJS with real-life practical examples and you will start loving it.

That’s all for Node JS and its Architecture and Node JS single-threaded event loop.

Learn NodeJS