Udemy logo

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();
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: [email protected]

Page Last Updated: June 2013

Node.Js students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo