Friday, August 12, 2011

Javascript On Server - I !


           Javascript is generally used to build web-applications. One can do whole lot of Front-End Stuff and give his/her application a interactive paradigm . Another aspect, can we do Javascript programming on backend or servers . Is event driven backend possible. Yes, Node.js is answer to all these question.
          In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does, by making use of Google's V8 VM, the same runtime environment for JavaScript that Google Chrome uses. Node.js is pretty good runtime enviornment coupled with usefull libraries. Javascript is pretty powerfull Event driven programming language.
          The “traditional” mode of web servers has always been one of the thread-based model. You launch Apache or any other web server and it starts receiving connections. When it receives a connection, it holds that connection open until it has performed the request for the page or whatever other transaction was sent. If it make take a few microseconds to retrieve a page from disk or write results to a database, the web server is blocking on that input/output operation. (This is referred to as “blocking I/O“.) To scale this type of web server, you need to launch additional copies of the server (referred to as “thread-based” because each copy typically requires another operating system thread).
              In contrast, Node.js uses an event-driven model where the web server accepts the request, spins it off to be handled, and then goes on to service the next web request. When the original request is completed, it gets back in the processing queue and when it reaches the front of the queue the results are sent back (or whatever the next action is). This model is highly efficient and scalable because the web server is basically always accepting requests because it’s not waiting for any read or write operations. (This is referred to as “non-blocking I/O” or “event-driven I/O“.)

Example :
  • You use your web browser to make a request for “/index.html” on a Node.js web server.
  • The Node server accepts your request and calls a function to retrieve that file from disk.
  • While the Node server is waiting for the file to be retrieved, it services the next web request.
  • When the file is retrieved, there is a callback function that is inserted in the Node servers queue.
  • The Node server executes that function which in this case would render the “/index.html” page and send it back to your web browser.
     V8 is constantly pushing the boundaries in being one of the fastest dynamic language interpreters on the planet. I can't think of any other language that is being pushed for speed as aggressively as JavaScript is right now. In addition to that, node's I/O facilities are really light weight, bringing you as close to fully utilizing your system's full I/O capacities as possible. In Nutshell, Event-Driven Nature, Scalable and modular are the few traits by virtue of which Node.Js is much talked about these days.

PS: DOM is browser things and till now has got nothing to do with Node. (Some work going in this direction)