Introduction to node.js
By: Blog Moove-It
Tags: Node Js, JavaScript, Web Developmen

I’m gonna talk about node.js and the benefits of using node.js on the server side. First lets clear some concepts.

Regular web servers use threads (OS threads) for handling requests.

1 request = 1 OS threads

1000 requests = 1000 OS threads

1 request = 1 OS threads 1000 requests = 1000 OS threads Every OS thread takes some memory from the system and does not return it till the thread execution is finished (i.e: servers the response).

Most of the time when doing I/O the thread just sits there and waits for the I/O operation to finish.

var result = query(“select * from users”)

//do something with result

The conclusion is simple, in order to serve applications with a lot of users making simultaneous requests or even holding requests from your web server, like chats or real time applications you can’t use regular web servers because it is impossible to scale.

Entering the loop

Ok, so there must be some other ways to handle simultaneous requests in an efficient way; yes there is a better way and is called the event loop.

Instead of using OS threads, the event loop uses green threads or co-routines. Co-routines cost less than regular OS threads and can switch from one context to another, preventing an unnecessary memory allocation.

Using green threads is much more efficient than using regular OS threads but still has some problems:

Context switching still costs

There is still machinery involved to create the illusion of holding execution on I/O

var result = query(“select * from users”)

We are still using this way of I/O

In order to code in an event loop you still need to know which functions will block and which ones won’t block.

It is difficult to combine the event loop libraries with regular, I/O blocking libraries.

Node js

Node js is a server implemented on top of the V8 the chromium (and chrome) javascript engine and in several ways is better than using an event loop system.

It uses one OS thread to handle multiple clients connections

Programs are written in plain javascript, server side javascript so, basically with node js we are using one language for client and server.

Does I/O better, because we are using javascript, for us javascript developers is easy to understand something like this:

db.query(“select * fro users”, function(data){

//Somethig with users

});

//Somethig with users }); We use callbacks all the time and node behaves this way every time I/O is performed.

File I/O is non blocking same as db queries.

It performs really well serving large requests simultaneously, comparable with specialized web servers like nginx, take a look at the presentations.

It will improve performance because Google will continue improving the V8 javascript engine.

Node js example

A node js program will be something like this:

var http = require('http');

http.createServer(function (req, res) {

res.writeHead(200, {'Content-Type': 'text/plain'});

res.end('Hello World

');

}).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');

Taken from node js site: http://nodejs.org/

As you can see even the server creation is using the callback approach.

Conclusion

Web development has change 180° since first static web pages appear, so there is no surprise that regular web servers evolve as well. Regular web servers based on OS threads are great to deliver occasional content in a fast and efficient way but they fail when we try to develop large rich web application that use the wire not only request data.

New “cool” developments like google talk on the browser, the facebook chat an many others are changing what we expect from the web to be, and we will need web servers capable of supporting this new features.

So let’s start playing around with node, or nginx or anything you feel comfortable with, and see what happens.

Resources

This presentation I did it here at moove-it. I also showed and coded some examples.

Reference

http://jsapp.us/(test your node js developments)