Archive | October 2011

Introducing node.JS


In general node.JS is a bunch of sugar on top of very complex virtual machine (V8) written by Google (V8 is a JavaScript engine inside of chrome) .It is a beast, it’s a huge, high performance virtual machine written by smart people .That’s not what node is.Its a set of libraries written on top of V8 that uses its greatness to do networking.

Installing node.JS

Just follow these steps for Unix-like platform.

$ wget http://nodejs.org/dist/node-latest.tar.gz
$ tar xzvf node-latest.tar.gz
$ cd node-v0.4.7
$ ./configure
$ make
$ sudo make install.

This in general is enough. After all that you can write the node command in your terminal. However if there are some problems while compiling it, you may refer to Git and the Node community for more help. Don’t forget that V8 can work only on x86, x64 and ARM architectures.

Interactive node.JS shell

If everything worked, you should be able to invoke the interactive node.JS shell ,you type node without arguments you get this prompt just as python and you could type real JavaScript in that

$ node
> 2+4
6

Node has a rebuild and you could type functions

> function add(a,b) { return a + b }
> add(5,4)
9

Now let’s just write a simple hello world program on node, that’s where we all start. Obviously you open up your text editor, I use vim.

$vim hello_world.js

e.g. helloworld.js

setTimeout(function() {
console.log(“world”);
}, 2000)
console.log(“hello”);

What this code does is…. let’s try, It prints hello and then waits for 2 seconds and then prints world. And then it exits.

Here what does setTimeout do?

SetTimeout has two arguments, First is call-back, and then a number of milli seconds you want it to wait.

Something that I need to say is,node should be somewhat familiar for JavaScript people.People who are familiar with browser side JavaScript should feel comfortable, not just for javascriptness but for the same pattern of coding.

The same helloworld.js can also be written in PHP as follows:-

e.g. helloworld.php

echo(“hello”);
sleep(2);
echo(“world”);

What is the difference between code in helloworld.js and helloworld.php?

In helloworld.js the hello happens immediately as it happened right through setTimout.here in helloworld.php we kind of stopped on the sleep.That is the fundamental difference.In node you never ever sleep, never have you fetched a URL from website and wait for it to come back.

You don’t have the ability to sleep on node and there is no possibility of halting executions, you can set Timeout to be ideal. In helloworld.js,it is not that CPU is spinning in busy loop,the CPU goes to zero and ideal and the OS unschedule’s it and schedules it back after timeout.Node is good at idling and it doesn’t sleep though.

Let’s improve on this example

What if i change setTimeout to setInterval?

e.g. helloworld2.js

setInterval(function() {
console.log(“world”);
}, 2000)
console.log(“hello”);

It prints

>hello
>world
>world
>and so on ..

This brings a important property of node:-it exits when there is nothing left to do. It has a natural ability to reference count the callback or intervals, it knows there is something to do, so it doesn’t dropout or once all gets done, it dropsout automatically.

These are some basic concepts of node.JS.

 

Advertisements