Node for a novice

So, this is my first blog and in this blog I would attempt to put down some stuff that I have understood or learnt or am learning or seem to have understood and putting together different pieces I have learnt from various sources also.

Having an understanding of JavaScript to some extent would be good to get started. If not you could read up more about it here. You might also reconsider reading the Wikipedia article on JavaScript before you read further.

So let’s get started! Okay firstly a quick intro to what this Node JS is all about. And then I started to see how useful this could be. Node JS or simply Node is used to provide an awesome way to build network programs and those that could be scaled. Now you might ask why not PHP? And how is this Node going to be of any use?

This is one very satisfying explanation you’ll find –

 What’s the issue with current server programs?

Let’s do the math. In languages like Java™ and PHP, each connection spawns a new thread that potentially has an accompanying 2 MB of memory with it. On a system that has 8 GB of RAM, that puts the theoretical maximum number of concurrent connections at about 4,000 users. As your client-base grew, if you wanted your web application to support more users, you had to add more and more servers. Of course, this adds to a business’s server costs, traffic costs, labor costs, and more. Adding to those costs are the potential technical issues — a user can be using different servers for each request, so any shared resources have to be shared across all the servers. For all these reasons, the bottleneck in the entire web application architecture (including traffic throughput, processor speed, and memory speed) was the maximum number of concurrent connections a server could handle.

So what does Node do and how is it any different? Instead of spawning a new thread for each connection  each connection fires an event, as node is event-run. Node will never deadlock, and I/O calls are non blocking and asynchronous. Awesome ain’t it? For a fact, Facebook still uses PHP majorly and not Node!In case you’re wondering why, Node is a recent technology and changing the technology stack is not a risk that companies would probably be taking!

Okay, so if you’re still reading this. You might be interested in knowing more! Here’s the official site :

There is documentation available on the site but you might not find the documentation quite useful if you didn’t know what to do with Node and what it’s meant for!

So let’s get to the point now! Simply put, Node is used as JavaScript on the server-side. Yes, I did not make a mistake, that’s right, on the server-side, outside your browser!!

Installation :

Okay cool! Let us check if you have node installed first. Type this on your command line :

node -v

You should now be seeing the version of node installed. Don’t have it installed? Follow the instructions  given here for the installation and we’re set!

Cross check if it works?

Open the Node Shell from the command line :

$ node 
> console.log('Hello World');  
  Hello World 

And now I suggest you go and read this. If you were a beginner like me, this is one perfect guide to make you an advanced novice in Node as the author says. 🙂 After this you should be able to do some cool stuff like writing an application that streams live twitter feed, maybe live feed about the location of visitors on your website if you own one!

And then once you are done, come back to visit my next blog which I hope to write soon where I’ll tell you about how I went on to use (Web Sockets) and MongoDB(which is a non-relational database) with Node and what they are all about , why I had to use MongoDB and not a relational database like MySQL and how I used these to build an application.

P.S : Since it’s my first blog, and me being a beginner, criticism in any form(although constructive would be preferred ) and comments suggesting any improvements would be highly appreciated.

Update (4th June 2012) : To convince yourself as to why Node is cool and a comparison as to how bad writing networking code in other languages is comparatively. Check this out.


5 thoughts on “Node for a novice

  1. I am extremely happy to note that your article was well written, without any glaring language error, and more importantly SMS speak (u gt wat im sayin rite?).

    As for Node.js – great to see you write about it. A question I’ve had and not bothered to find out – why Javascript? Handling connections in an asynchronous event driven fashion isn’t rocket science – wouldn’t a similar approach in a more “efficient” language be better?

    • Thanks a lot for the feedback.

      And as to why JavaScript? I tried to figure this out. And V8 Engine is awesome. A quick glance at the Wikipedia article and this is what I found –

      “V8 increases performance by compiling JavaScript to native machine code (x86,[2] ARM,[2] or MIPS[3] CPUs), before executing it, versus executing bytecode or interpreting it. Further performance increases are achieved by employing optimization methods such as inline caching.”

      I am assuming this is one major reason for choosing JavaScript over other languages.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s