[caption id="" align="alignright" width="635"] Ryan Dahl[/caption]
He started developing it with some main points in mind:
- event driven mechanism
- non blocking IO
- Scalability
- performance and speed
JavaScript would obviously seem to be the best option to create an event driven technology. the fact that node.js is event driven, and that it has a non blocking IO is pretty connected, the event mechanism is used to make callbacks everywhere, so a basic file read instruction for instance, can be non blocking by setting what to do with the data grabbed from the file as a callback function, so that while the system is reading the file, it can continue executing the script and serve the data when it comes. Take thin example:
var fs = require('fs');
var contents = fs.readFileSync('users','utf8');
console.log(contents);
console.log("Hello Node\n");
var contents = fs.readFileSync('hosts','utf8');
console.log(contents);
console.log("Hello again!");
this code is blocking, means every instruction is executed only if its predecessor is fully executed. This approach is what believed to be non network friendly, the node.js way to do it is:
var fs = require('fs');
var contents = fs.readFile('./users','utf8', function(err,contents){
console.log(contents);
});
console.log("Hello Node\n");
var contents = fs.readFile('./hosts','utf8', function(err,contents){
console.log(contents);
});
console.log("Hello again!");
You have to know that, when you try this code, you will get an unexpected result, the 2 instructions that do not depend on the file system read call are executed in the first place,
Hello Node
Hello again!
list of users...
list of hosts...
this weird order comes from that the list of users and the list of hosts have to be read first, so that would take time, so we printed them in the callback of the read function, in other words, read the file, once done echo it, but meanwhile, do something else.
Best part of node.js is that it doesn't rely on an external software to do the web server part, it's the web server, the website and everything in the same time! that allows to build fast, scalable and flexible web applications with minimum overhead.
But what about performance? node.js is built on top of Google's V8 JavaScript engine, the libuv platform abstraction layer, which are written in C++ and C, the world fastest programming languages. That would have huge effect on the time and resources needed in order for node.js to interpret the code. I don't think there would be a better solution.
Conclusion
node.js is really useful and it has a great future. It's easy to learn and everyone should at least know about it. I'll be posting tutorials and open source projects I have written using node.js, so follow me and stay tuned! Meanwhile take a look at node.js website.
The image at the top is really nice. You made it yourself?
ReplyDeleteNo, the photo was taken in a Node.js conference along with many others, here is a link for the entire album http://www.flickr.com/photos/franksvalli/with/5163817732/
ReplyDelete[…] have already introduced node.js in a previous article and explained how to install it in another one. Now it is time to go further with it. Basically […]
ReplyDelete