James Derulo's

Portfolio

Understanding Asynchronous function and Event Loop in Node.js

Leave a Comment
I built some basic notes to a kick-start on Node.js and which turns out to be very interesting platform and allow you to have full server-side control through java script. I have developed some notes over the period of time and as I discussed here  a blog post about getting started with Node.js, I would like to add some more content moving forward but this time I am focused here to demonstrate some nature of Node.js and the patterns it leverage


Pic Credit :http://www.michaeld.me/

What is Node.js ? 

Node.js is a server-side technology that's based on Google's V8 JavaScript engine. It's a highly scalable system that uses asynchronous event-driven I/O (input/output), rather than threads or separate processes.

It's ideal for web applications that are frequently accessed, but computationally simple. If you're using a traditional web server, such as Apache, each time a web resource is requested, Apache creates a separate thread or invokes a new process in order to process the request.

Once you install Node.js in your machine here are some basic commands and quick application that you can built in no-time that will help you building application over Node.js

Running Node

Once you install, Node.js package in your machine, go to your CLI (command line interface) or terminal and I strongly recommend to download the third-party terminal called ITerm in macintosh.
Type in    - $ node
This will invoke CLI, which will wait for you to input to an expression
>console.log('Welcome to Node.js'); Welcome to Node.js
Alternatively, you can run Node.js from a simple javascript file, like you create an javascript file with this content ' console.log ('Running Node from file'); .  If you name the file as application.js then you can invoke the file as
> node application.js    --To quit CLI , type in CTRL+C (on your mac machine)

Understanding Node.js functionality through a working application

You can create your javascript file as well, and here I created a file called kissmyapp.js that greets the user whoever access this file.
To run this application save this application as kissmyapp.js and to run this application use
| $ node kissmyapp.js
| $ Server running at 8080

Asynchronous function and the Node Event Loops 

Likewise in Apache, there are two ways to handle the incoming requests,


  • First approach would be (prefork or Multi Processing Model - called MPM) this approach assign each request to separate process until the request is satisfied, the benefit of this approach would be that the application need not have to be thread safe while disadvantage is each process is memory intensive and which clear do not scale so well
  • Second approach is (worker MPM) which spawns thread for each request, so each new incoming request is handled via a new thread and clearly is more memory inefficient but the application have to be thread-safe, PHP on the other hand is not thread safe
Regardless of all, both the types respond to request in parallel, so clearly with fifty people request a server at same time, the web-server handles all request simultaneously 

How does the Node works ?

When you start a node application, it is created on single thread of execution, so clearly it sits there and wait for the first request to come. When it gets a request , then no other request is processed unless first process is finished. Now this many sound very in-efficient, yes but except for one-thing.

Node operates asynchronously, via an event and callback functions the events polls for each events and invoke event handlers for each event so in Node the callback function is event handler

nodejs for dotnet
Picture Credit : http://rickgaribay.net/

Node processes the request, but doesn't wait around until the request receives a response. Instead, it attaches a callback function to the request. When whatever has been requested is ready (or finished), an event is emitted to that effect, triggering the associated callback function to do something with either the results of the requested action, or the resources requested.

If five people access an application at the exact same time, and the application needs to access a resource from a file, Node attaches a callback function to a response event for each request. As the resource becomes available for each, in turn, the callback function is called, and each person's request is satisfied. In the meantime, the Node application can be processing other requests, either for the same people, or different people.

Node application that read file asynchronously 


What happened here ?

When the connection is established, a listening event is emitted, which then invokes the callback function—outputting a message to the console. Accessing a file is a time consuming operation, relatively speaking, and a single threaded application accessed by multiple clients that blocked on file access would soon bog down and be unusable.

Instead, the file is opened and the contents read asynchronously. Only when the contents have been read into the data buffer—or an error occurs during the process—is the callback function passed to the readFile method called. It's passed the error, if any, and the data if no error occurs.

In the callback function, the error is checked, and if there is no error, the data is then written out to the response back to the client. 
Most people who have developed with JavaScript have done so in client applications, meant to be run by one person at a time in a browser. Using JavaScript in the server may seem odd. Creating a JavaScript application accessed by multiple people at the same time may seem even odder. 

More on Node 

Node is very efficient in handling multiple request from browser, in my next post i'll also be writing an application that creates a client and server and clients sends multiple request to server, like when we request a blog application to show a post , in the meantime time we request to show favicon file, the Node handles this fairly smoothly and then later part would be building some cools application using Express.js which I have been testing in a while

Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment