NodeJS Continues To Shine

Node.js celebrates its tenth anniversary this year (2010-2019). That life span for all intents and purposes will ascribe the technology as fairly new. Yet, it has already been recognised as a top-notch solution for many web based projects. This is due mainly to its open-source and cross-platform JavaScript runtime environment. Javascript now does a whole lot more than interactions on your web pages. In fact, it makes it possible to develop applications with a single programming language, rather than use different languages for server and client-side scripts. That is a very Big Step Change in Web Application Developmemt And Deployment.

What is Node.js, exactly?

Before Node.js web apps were written in a client/server model - herein the client (browser) would demand or request certain resources from the server. The server would respond by passing on the requested resources. The server only respond when a client makes a request after which the connection is closed immediately. In 2009, Ryan Dahl introduced a new approach to server-side runtime written in JavaScript. It enables requesting in and out of the web server (I/O) to be processed concurrently and asynchronously using a concept called non-blocking, or asynchronous I/O. And that is how Node.js got started.

The original idea behind the use of Node.js was to build websites with real-time push capability. Unlike the standard client/server model where a request is made and the client has to wait for a response - very uni-directional as illustrated below, it became possible to develop two-way connection web applications (websites) with free data exchange on a continous bases.

This is reliant mostly on WebSockets, which allow opening an interactive communications session between a user’s browser and a server. Requests to a server are then processed as a loop (event loop, to be more precise), which makes Node.js a JavaScript runtime environment that operates on a “non-blocking” approach to serving requests and thus, achieves low latency and high throughput along the way - very bi-directional as illustrated below.

What is a WebSocket?

A WebSocket is a computer communications protocol, a system of rules that allow two or more devices to exchange information and data. The well known internet standards http and https are also communications protocols which send and receive information via requests and responses as already mentioned above. A protocol defines the rules, language, semantics and rate of synchronization of the exchange of info. It may also include details of and possible error recovery methods.

Basically, a Node.js app is run in a single process, without creating a new thread for every request as depicted in the bi-directional graphic above. It makes such apps very efficient, mostly thanks to the modern javascript engines that run on all standard modern web browsers. The V8 JavaScript engine in Chrome was the pioneer in this endevour. . The engine takes the JavaScript code and converts it into lower level or machine code (through a transpiler) - which as a result, does not need to be interpreted first in order to be run. Running code on the server this way actually increases the processing speed.

Why use Node.js as your backend?

JavaScript Background & Prevalence

Since its inception in 1995, JavaScript has been growing into the most popular, dominant language of the web. Over the last decade, the adoption of JS has only been growing, showing no signs of abating. According to StackOverflow’s annual survey, 69.8% of respondents and 71.5% of professional developers are using JavaScript today. With the prevalence of more web-enabled devices, the race to come up with the best "one size fits all solution" grows bigger.

It is worth keeping in mind that JavaScript is supported by all modern web browsers and hence employed by all modern websites. As a matter of fact, it has been the most commonly used programming language valued especially for its simplicity and versatility. With Node.js JavaScript is not limited to the front-end but works outside of the browser as well. As a result, it is possible to build fast and scalable applications with the same code for both server- and client-side. What is more, with frameworks such as Electron or NW.js, it is possible to develop native desktop applications for Linux, Windows and MacOS, using web technologies like HTML, CSS and JavaScript.

Node.js is a perfect choice for data-intensive, real-time apps that run across distributed devices. Take chats, as an example. With Node.js, you can easily serve multiple users and devices, and handle both big traffic and intensive data exchange. Speaking of data - it is also possible to leverage the fact that HTTP requests & responses are essentially data streams, and stream large files or even process them while they are being uploaded (to encode audio & video, for example). It is also a powerful solution for collecting and visualising real-time data in the form of advanced dashboards. GraphQL is an interesting example. Although this query language for APIs can work together with other programming languages, such as Python or Java, it’s Node.js that helped GraphQL to gain traction.

No matter the common uses of Node.js, it is also worth to keep in mind that there is a robust and helpful community behind it. It makes it much easier to solve any problem at hand - not to mention that Node package manager (npm), which is included in Node.js by default, is said to be the fastest growing and one of the largest ecosystem of open source libraries in the world. Node.js also comes with fine documentation.

On the other hand, Node.js is not the best choice for handling CPU-intensive apps. Take editing graphics as an example - it requires an efficiency that an event-based and a single-threaded environment can’t really provide. Itis also being criticised for not being mature enough - at least not just yet. Even though the main Node.js modules are stable, there are many entries in the npm registry which are low-quality or just haven’t been tested and/or documented properly. The examples of Node.js applications Despite a few drawbacks, the words that best describe Node.js by its users happen to be: fast, simple, and easy - but also powerful and flexible. It might be the reason why Node.js is widely used by both emerging startups and some well-known companies.

Rumour has it that Netflix and PayPal have already implemented Node.js, and saw significant results afterwards - both economically and in performance. Actually, Node.js is said to boost business productivity by the majority of companies that decided to take advantage of it. It also powers the two biggest online marketplaces - eBay and AliExpress (and Amazon on top of that). While there’s still some debate about the maturity of Node.js, its popularity is still growing. In fact, it was downloaded more than 360 million times in 2018 - and all versions of Node.js show a 40% year-by-year growth of downloads. Given these numbers, it’s likely that there will be more great Node.js examples in the future as well. Especially since Node.js is scalable and relatively easy to learn and use, which often matters for startups.