Node.js shines in real-time web applications employing push technology over web sockets. What is so revolutionary about that? Well, after over 20 years of stateless-web based on the stateless request-response paradigm, we finally have web applications with real-time, two-way connections, where both the client and server can initiate communication, allowing them to exchange data freely. This is in stark contrast to the typical web response paradigm, where the client always initiates communication. Additionally, it’s all based on the open web stack (HTML, CSS, and JS) running over the standard port 80.

What is Node.js?

Node.js – an open-source Chrome’s Javascript runtime environment – allows you to develop scalable web applications effortlessly. This environment is built on top of Google Chrome’s JavaScript Engine V8. It uses an event-driven, non-blocking I/O model, making it lightweight, more efficient, and perfect for data-intensive real-time applications running across shared devices.

Before proceeding further, here are two facts that you must know.

  • Node.js isn’t a programming language or framework; instead, it’s an environment for them.
  • Contrary to the popular opinion that Node.js is used for building servers only, the truth is – it can be used on the front and backend both.

node.js

How Does It Work?

The main idea of Node.js: use non-blocking, event-driven I/O to remain lightweight and efficient in the face of data-intensive real-time applications that run across distributed devices.

What it means is that Node.js is not a silver-bullet new platform that will dominate the web development world. Instead, it’s a platform that fills a particular need. And understanding this is essential. You don’t want to use Node.js for CPU-intensive operations; in fact, using it for heavy computation will annul nearly all of its advantages. Where Node shines is in building fast, scalable network applications, as it’s capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability.

How it works under the hood is pretty interesting. Compared to traditional web-serving techniques where each connection (request) spawns a new thread, taking up system RAM and eventually maxing out at the amount of RAM available, Node.js operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections held in the event loop.

node.js

A quick calculation: assuming that each thread potentially has an accompanying 2 MB of memory with it, running on a system with 8 GB of RAM puts us at a theoretical maximum of 4,000 concurrent connections (calculations taken from Michael Abernethy’s article “Just what is Node.js?”, published on IBM developerWorks in 2011; unfortunately, the article is not available anymore), plus the cost of context-switching between threads. That’s the scenario you typically deal with in traditional web-serving techniques. By avoiding all that, Node.js achieves scalability levels of over 1M concurrent connections, and over 600k concurrent WebSockets connections.

There is, of course, the question of sharing a single thread between all clients’ requests, and it is a potential pitfall of writing Node.js applications. Firstly, the heavy computation could choke up Node’s single thread and cause problems for all clients (more on this later) as incoming requests would be blocked until said computation was completed. Secondly, developers need to be careful not to allow an exception bubbling up to the core (topmost) Node.js event loop, which will cause the Node.js instance to terminate (effectively crashing the program).

The technique used to avoid exceptions bubbling up to the surface is passing errors back to the caller as callback parameters (instead of throwing them, like in other environments). Even if some unhandled exception manages to bubble up, tools have been developed to monitor the Node.js process and perform the necessary recovery of a crashed instance (although you probably won’t be able to recover the current state of the user session), the most common being the Forever module, or using a different approach with external system tools upstart and monit, or even just upstart.

Features Of Node.js

Node.js has grown quickly in the last few years. This is thanks to the vast list of features it provides:

  • Easy – Node.js is quite easy to start with. It’s a go-to choice for web development beginners. With a lot of tutorials and a large community – getting started is very easy.
  • Scalable – It provides vast scalability for applications. Node.js, being single-threaded, is capable of handling a huge number of simultaneous connections with high throughput.
  • Speed – Non-blocking thread execution makes Node.js even faster and more efficient.
  • Packages – A vast set of open-source Node.js packages is available that can simplify your work. There are more than one million packages in the NPM ecosystem today.
  • Strong backend – Node.js is written in C and C++, which makes it speedy and adds features like networking support.
  • Multi-platform – Cross-platform support allows you to create SaaS websites, desktop apps, and even mobile apps, all using Node.js.
  • Maintainable – Node.js is an easy choice for developers since both the frontend and backend can be managed with JavaScript as a single language.

node.js

Why node.js?

Node.js is good for prototyping and agile development. It also allows you to build fast and highly scalable applications.

Node.js has a large ecosystem of open source libraries so that you can reuse existing libraries and spend more time focusing on your applications. If you already use JavaScript for front-end development, you can leverage your existing skills for server-side development. By using the same JavaScript on both client-side and server-side, your codebase is cleaner and more consistent.