Monday, December 8, 2014

Node.js

Node.js is an open source runtime environment / platform that allows advanced programmers to build scalable, high-performance network applications.

Since lately there is a lot of node.js buzz in the Miami start-ups community, I decided to write this blog & explain the technology in a little more details. This blog does not want to be a comprehensive a node.js manual but it will give new developers and business community an idea about this relatively new technology.

Node.js gets implemented server side (i.e. on a Linux machine), it's based on javascript and it uses an event-driven, non-blocking I/O model which is very good for data intensive real time applications.

Node.js was created by an individual by the name of Ryan Dahl in 2009. It is currently gaining popularity and it is used by some large companies like Microsoft, Yahoo, Walmart, Groupon and Paypal.

The story says that Dahl was inspired after looking at a large file upload progress bar on Flickr. The browser could not assess what percentage of the file was uploaded and it had to query the server to retrieve that information.

Node.js can keep many connections alive without having to reject the new incoming connections. It can be compiled on the linux server and, many of these enhanced packages/libraries, it can also be downloaded as a pre-compiled library/binary. Combined with a  document database (such as MongoDB) node.js offers a unified javascript development stack.

Here are a little more details about the philosophy of non-blocking code:

Blocking code looks like this (in some form of pseudo code):

Read file from Filesystem {
Store content
Print content
}
Do something else

Non-blocking code looks like this (in some form of pseudo code):

Read file from Filesystem {
Whenever you complete, print content
}
Do something else

While apparently there is not much of a difference between these two different ways of serving things, if you sit down and draw time diagrams of these calls you will notice significant differences in performance.

Let's say we have 2 calls: one that takes 7 seconds, the other one takes 3 seconds.

When you run these two different calls in a blocking manner they will be sequentially served and the total time to process them will be 10s. When you run the same two calls in a non-blocking manner they will be served in parallel and the total time to process them will be 7s resulting in a 30% improvement in performance. The more concurrent calls you have, the more you will improve.

Syntax wise, on a standard linux machine:

Blocking

var content = fs.readFileSync('/path/to_filename');
console.log (content);
console.log ('Keep going');

Non-blocking

fs.readFile ('/path/to_filename', function (content, err)) {
console.log(content);
}
console.log ('Keep going');

Please notice the difference in the names of the system functions, readFile for non-blocking (or async) vs. readFileSync for blocking.

This example is just illustrating the concept on reading a file but similar code constructions can be built to consume web services, start/stop web servers, refresh web pages in real time etc. With node.js the events treated in the loop are always asynchronous. Typical blocking operations such as calls to web services / apis, reads / writes from / to databases or calls to extensions can now be accelerated.

Here are some types of applications you can build using node.js:

(1) Chat servers
(2) Social media networks
(3) Fast files upload solutions
(4) Ad servers
(5) High volumes e-commerce sites
(6) Collaboration tools
(7) Mobile apps that talk to a db via an API

Here are some types of applications where node.js is not the best fit for:

(1) CPU intense applications / algorithmic applications [i.e. it won't be the best idea to write a chess game in node.js]

Make it a great day!

Adrian Corbuleanu
Miami Beach, FL
http://wittywebnow.com

To document for this article I used the following online resources:

1. http://nodejs.org
2. http://codeschool.com (node.js for dummies)
3. http://stackoverflow.com
4. http://wikipedia.com