Why You Should Learn Node.js Today

image00Let’s talk about a technology that has been getting a lot of well-deserved hype lately: Node.js. Node.js is the hottest new technology in Silicon Valley. Currently in use by Microsoft, VMWare, Ebay, Yahoo, and many more top tech companies, Node.js is the perfect skill to open up amazing career opportunities for any software developer.

You may have heard of Node.js, but know only that it has something to do with “real-time” or “highly scalable” apps. You may have heard that Node.js is JavaScript for the server-side (and you may be wondering why anyone would want that!). Or maybe you know exactly what Node.js is, but aren’t sure when or why to use it. Just sit back, and I’ll explain everything.

Let me start at the beginning.

The Web is Changing

The web used to be about consumption. Viewing web pages, watching videos, looking at pictures of cats. Of course, its still about pictures of cats…but the web has become more about interaction. Users around the world want to interact with each other, and they want to do it in real time. Chat, gaming, constant social media updates, collaboration – each of these features requires real time communication between users, clients, and servers across the web. What’s more, this real-time communication needs to happen at massive scale, supporting hundreds, thousands, even millions of users.

So what do software engineers need to make this happen? We need real-time communication between clients and servers – which means we need fast, persistent I/O. Anyone with web development experience knows that HTTP wasn’t built with this use case in mind. Large numbers of clients continuously polling a server simultaneously is incredibly slow and inefficient. To enable scalable real-time communication, servers need to be able to push data to clients, instead of HTTP’s heavy request/response model. We also need to make sure that these lightweight push communications work in a way that is scalable, maintainable, and usable from a software development standpoint.

Enter Node.js

Node.js is an event-driven, server-side JavaScript environment. Node runs JavaScript using the V8 engine developed by Google for use in their Chrome web browser. Leveraging V8 allows Node to provide a server-side runtime environment that compiles and executes JavaScript at lightning speeds. The major speed increase is due to the fact that V8 compiles JavaScript into native machine code, instead of interpreting it or executing it as bytecode. Node is open source, and cross-platform, running on Mac OSX, Windows, and Linux.

But JavaScript? On the server-side? Why? Though JavaScript has traditionally been relegated to menial tasks in the web browser, it’s actually a fully-functional programming language, capable of anything that more traditional languages like C++. Ruby, or Java, are. Furthermore, JavaScript has the advantage of an excellent event model, ideal for asynchronous programming. JavaScript is also a ubiquitous language, well known by millions of developers. This lowers the learning curve of Node.js, since most devs won’t have to learn a new language to start building Node.js apps.

**Learn Node.js Today! Check out this on-demand, online course.**

Asynchronous Programming The Easy Way

In addition to lightning fast JavaScript execution, the real magic behind Node.js is something called the Event Loop. To scale to large volumes of clients, all I/O intensive operations in Node.js are performed asynchronously. The traditional threaded approach to asynchronous code is cumbersome and creates a non-trivial memory footprint for large numbers of clients (each client spawns a thread, the memory usage of which adds up). To avoid this inefficiency, as well as the known difficulty of programming threaded applications, Node.js maintains an event loop which manages all asynchronous operations for you. When a Node application needs to perform a blocking operation (I/O operations, heavy computation, etc) it sends an asynchronous task to the event loop, along with a callback function, and then continues to execute the rest of its program. The event loop keeps track of the asynchronous operation, and executes the given callback when it completes, returning it’s results to the application. This allows you to manage a large number of operations, such as client connections or computations, letting the event loop efficiently managing the thread pool and optimize task execution. Of course, leaving this responsibility to the event loop makes life particularly easy for Node.js developers, who can then focus on their application functionality.

The Node.js Event Loop Lifecycle

This capability to simplify asynchronous programming is what makes Node.js such a powerful tool for developers. With Node.js, you can build complex applications that can scale to millions of client connections because the application handling client requests is passing off all of the time-intensive work of managing I/O and computation to the event loop.

**Learn Node.js Today! Check out this on-demand, online course.**

Community Driven

In addition to it’s innate capabilities, Node.js has a thriving open source community which has produced many excellent modules to add additional capabilities to Node.js applications. One of the most famous is Socket.io, a module to manage persistent connections between client and server, enabling the server to push real-time updates to clients. Socket.io abstracts the technology used to maintain these connections away from the developer, automatically using the best technology available for a particular client (websockets if the browser supports it, JSONP or Ajax longpolling if not). This amazing technology allows you to program real-time applications simply, with very little code. For example, take this highly-scalable, real-time chat server:

server = require('http').createServer();
server.listen(8080);

var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket){

    // when message received, send it to all connected clients
    socket.on('message', function(message){
        console.log("Received message: " + message + 
                    " - from client " + socket.id);
        io.sockets.emit('chat', socket.id, message);
    });    
});

Just 10 lines of code! The accompanying client-side code is just as simple. Just think of the possibilities! Your future writing scalable, real-time applications begins now, with Learn Node.js By Example. With detailed screencasts, tutorials, and in-depth coding exercises, this highly-rated course will have you writing Node.js applications in no time.

For a limited time, subscribe to Learn Node.js by Example now and get 30% off!

About the Author

image01Constantine Aaron Cois leads a software engineering team at Carnegie Mellon University. In addition, he is an Adjunct Professor teaching graduate-level software engineering classes. Aaron obtained his Ph.D. in 2007 developing advanced algorithms and software systems for biomedical applications. His current areas of work are web applications, real-time systems, and cloud computing.

Blog: http://www.codehenge.net

Email: cacois@gmail.com

Comments

  1. Thileepan Sivanandham says:

    Very nice! Thanks for this psot.

  2. Excellent pos, thanks!

  3. I believe node.js is single threaded?

  4. I'm the last person who should be commenting on it, but I believe the event loop is, but it's also non-blocking and lets you spin off threats for blocking processes.

  5. Waldir Borba Junior says:

    Fantastic. Thank you

  6. Nodejs is bright and powerful, but we must stop to convince the people that there is only nodejs we can make asynchronous, it is not brand new either.

  7. Liz Sabrina says:

    like the post.thanks.

  8. My next thing…I lve node.

  9. “I used to be an adventurer like you, then I took Node.js in the knee.”.

  10. excellent post

  11. Awesome post……Thanks…

  12. I think it has to make sure to not run JS handlers cross-thread. Each thread does non-preemptive multitasking, just like in the browser. And, just like in the browser, different threads handle different tasks, and they're independent of each other. Each thread has its own internal space of objects. So in the middle of request A, it must read file X. The thread opens A and sets a callback for when the reading is done. The actual reading of file X might be done in a separate thread, but there's no JS being run in that thread; it's done for you. The callback must be run in the same thread as A after the file is read; if needed, it has to wait for some other event handler on that thread to finish, because you can't have two threads changing the same data at once. I've done multi-threaded programming before; it's madness.

  13. great article.

    im new in Node.js but I wrote a simple example to capture form data.any one can checked : http://www.stirring-interactive.com/blog/node-js-in-a-nutshell/.

  14. Yes a single instance of node runs on a single thread, but you can easily solve this limitation using clusters with cluster.js creating a cluster of tasks or processes.

  15. Hey Thanks for the node overview. Will try node with mongodb on my next web app.

  16. This is a great article, very clearly written.

    I do get an understand the value of node.js, but some of its benefits are overstated. Consider a case where I make an async call for a database query. That database query is still an HTTP call and still requires a thread to be waiting for the response. In this case I have simply delegated the responsibility of waiting for the I/O to a thread other than the calling thread, and added the overhead of the "event loop".

    Node.js really became popular because of all the new content rich sites that are using server push patterns. You don't want to hold onto threads on the server while you wait for your event to happen. However, java servlet spec 3.0 includes async servlets that accomplishes the same task and the argument of async calls and "event driven programming" are not a valid argument over J2EE apps.

    The real big draw to Node.js now is what was already stated here, programmers can write server and client side code with javascript and you only need 1 language. I get that. node.js is fine for small server apps, I would not want to use this language for massive enterprise applications, the javascript code is too hard to debug. You lose your stack every time you make an async call.

  17. Awesome post!

  18. Awesome post ever

  19. Excellent post. Thanks a lot.

  20. Abdullah Maswadeh says:

    Excellent post . nodeJS saved my ass on long process request.

  21. Meera Nikhil Kalantre says:

    great…with lots of information…and its guideline to all dev's :)

Speak Your Mind

*

Email