Friday 4 March 2011

Yet Another Event-driven Post

If you follow us, you have certainly caught our previous post (from Miguel) about Libevent. I must say, Libevent seems to be really cool, but it is still C. And since only a few of us like C, and it almost forces us to use threads (which means more resource consumption and more complexity) to perform what prove to be simple tasks on more high-level languages, how come we can do all these things more easily?

So how about leaving Libevent to the Gurus behind database and filesystem access driver libraries development, and focus on a single-threaded powerful non-blocking event loop programming style?

Welcome to the wonderful world of Node.js!

At a glance, Node is a JavaScript server-side programming environment (framework style) that provides the ability to handle server requests and responses, be it HTTP or raw TCP, with a seamless event-driven approach with those JavaScriptish callbacks leveraging a non-blocking I/O event loop. Being JS-based, Node also inherits all the document processing tools for the client-browser-app's DOM, and a bunch of other cool stuff that allows you to develop kick-ass web applications using Javascript sometimes all over the stack (hello MongoDB!!).

To show you how painless Node can be, here is the implementation of the infamous chat server self-learning example:
1:  // this is how you load the "net" module (that encompasses the TCP utilities)
2: // it's actually better to assign it to some variable and use it throughout the code
3: net = require('net');
4:
5: // connected sockets pool
6: pool = [];
7:
8: // create a TCP server instance (using the "net" module)
9: server = net.createServer(function(socket) {
10: // add client socket to pool
11: pool.push(socket);
12: // listen on client's socket for incoming data
13: socket.on('data', function(content) {
14: for(var i = 0; i < pool.length; i++) {
15: // send message to all clients
16: message = socket.remoteAddress + ' > ' + content
17: pool[i].write(message);
18: }
19: });
20: // remove inactive sockets from pool
21: socket.on('end', function() {
22: var i = pool.indexOf(socket);
23: pool.splice(i, 1);
24: });
25: });
26:
27: // run server on port 8000 (or other)
28: server.listen('8000');

There are roughly 20 lines of code, and it is actually readable!! Are you serious?!?! Goodbye Erlang and other funky stuff (just kidding here).

I am also just entering this wonderful world, so there is not much more I can say to you about it. So why don't you check the links below for more info?


Feel free to leave more interesting resources in your comments (please do). Nonetheless, we will be tracking Node's evolution closely here, it's really promising stuff.

Kudos to Ryan Dahl, the man that made it all possible.

1 comment:

  1. Great and useful post about Node.js! :-)

    I am aware of the power and simplicity of Node.js, unfortunately I didn't yet had time to deepen my knowledge in this area...

    Well, just want to a comment. You can write code for the same purpose of the example in Erlang in the same number of lines or not so much more. But if you're not familiar with functional programming..Ok, maybe you'll need some patience to understand it ;-)
    But despites that, do not forget that Erlang deals with concurrency way better than Node.js. And it also has a network-transparent parallel distributed system.

    But indeed, Node.js is powerful and seems to be gaining a lot of users and uses!

    ReplyDelete