Node.Js = { Asynchronous + Non-blocking + Events + Callbacks }
Jul 10, 2012
Suresh Vasudev
3 minute read

Synchronous and Asynchronous I/O

Before getting into how to use Node.js let us brush up our memory about synchronous and asynchronous I/O operations

To understand better consider the case of a File I/O operation in synchronous and asynchronous mode.

npm

In Synchronous mode, the thread waits for I/O to complete before proceeding further. The thread will be in a “wait” state.

However in the case of asynchronous operation, the thread will not wait for I/O to finish. It will continue with the processing till it gets interrupted (once the File I/O operation is over).

Node.js application runs in a single thread and it dictates that developer should use an asynchronous style of coding. Coding in an asynchronous way is a style of programming and it is the duty of the developer to make the best use of it in node.js

Event Driven Programming and Callbacks

Web pages are interactive in nature. The web responds based on user interaction like clicking a link, submitting a form etc and it is known as “Event-driven”. The events are handled in web using JavaScript.

Callbacks are functions that are passed as an argument to another function. In an Event driven programming architecture, there will be a provision to subscribe to an event by passing a callback function along with subscription. The callback function will be invoked when that event happens.

Javascript works on this principle of event driven programming architecture. Node.js, being a server side JS framework, developer MUST make the best use of this JS feature. This way we can write efficient non-blocking codes.

Node.js always invoke callbacks in asynchronous mode,with no guarantee in theorder of execution of event callback functions.

Web pages are interactive in nature. The web responds based on user interaction like clicking a link, submitting a form etc and it is known as “Event-driven”. The events are handled in web using JavaScript.

Callbacks are functions that are passed as an argument to another function. In an Event driven programming architecture, there will be a provision to subscribe to an event by passing a callback function along with subscription. The callback function will be invoked when that event happens.

Javascript works on this principle of event driven programming architecture. Node.js, being a server side JS framework, developer MUST make the best use of this JS feature. This way we can write efficient non-blocking codes.

Node.js always invoke callbacks in asynchronous mode.,with no guarantee in theorder of execution of event callback functions.

[javascript]

var http = require(‘http’);

function getSite(site) { http.get(site, function(res) {

console.log(“Got response: [“+ site +”] “ + res.statusCode); }).on(‘error’, function(e) { console.log(“Got error: “ + e.message + site); }); }

var websites = [”http://www.google.com”, “http://www.yahoo.com”, \ “http://www.facebook.com”, “http://www.nodejs.org”, “http://www.orcale.com”]; for (var i=0; i< websites.length; i++) { //console.log(websites[i]); getSite(websites[i]); }

[/javascript]

Let us execute the above application “n” times and see the output. We can see that every time the order of response from each web site is different and it is reflected in the output.

npm

Summary

  1. Node.js is a server side JS framework
  2. Node.js is good for effecitly implementing non-blocking I/O operations
  3. Node.js uses Event Driven and Callback architecture for implementing asynchronous operations.