Node Js
What Is Node.js?
Node.js is an open source, cross-platform runtime environment and library that is used for running web applications outside the client’s browser.
It is used for server-side programming, and primarily deployed for non-blocking, event-driven servers, such as traditional web sites and back-end API services, but was originally designed with real-time, push-based architectures in mind. Every browser has its own version of a JS engine, and node.js is built on Google Chrome’s V8 JavaScript engine. Sounds a bit complicated, right?
In simple terms, what this means is that entire sites can be run using a unified ‘stack’, which makes development and maintenance quick and easy, allowing you to focus on meeting the business goals of the project.
The fact that Node.js is open source means that it is free to use and constantly being tweaked and improved by a global community of developers.
An important thing to understand about Node.js is that it is actually neither a framework or a library - as with traditional application software -, but a runtime environment.
A runtime environment (sometimes shortened to RTE) contains Web API’s that a developer can access to build a code, and a JavaScript engine that parses that code. This makes it lightweight, flexible and easy to deploy, all features that will help to optimize and speed up your application project.
Node.js architecture
The mechanics of Node.js are what contributes to its popularity with developers. Whereas most alternative runtime environments utilize multi-threaded processing models, Node.js does it all in a single thread.
In multi-threaded processing setups, each server has a limited thread pool it can access. So every time a server receives a request, it pulls a thread from the pool and assigns it to that request, to take care of processing it. In this case, the processing is synchronous and sequential, which means that one operation is performed at a time.
In multiple-thread processing, a thread is picked out every time a request is made until all of the limited threads are used up. When this happens, the server has to wait for a busy thread to become free again. This can make for slow and inefficient applications, which leads to knock-on effects on anything from customer experience to lead conversions. It can particularly become a problem if your application has to deal with a high number of concurrent client requests.
Node.js, however, uses single-threaded processing. The difference between the two is as you’d imagine: single-thread architectures process every request using a single main thread, utilizing event loops to run blocking Input/Output operations in a non-blocking way. Don’t worry if some of these terms seem unfamiliar. The ‘Terms To Know’ section, below, will explain it all in more detail.
A single-thread architecture can, in theory, perform and scale much more quickly and efficiently than multiple-thread setups. This is what Ryan Dahl had in mind when he first wrote Node.js and is a big part of why it is so popular among web application developers.
Node.js terms worth knowing
Below is a list of common terms you’ll see when reading about or discussing Node.js, and their definitions.
What is JavaScript?
JavaScript is the programming language that underpins Node.js. It is a high-level and multi-paradigm language characterized by ‘curly bracket’ syntax, dynamic typing, prototype-based object orientations and first class functions.
These features mean JavaScript can convert a static webpage to an interactive one, adding features such as search boxes, embedded videos or news feed refresh tools. These are features that aim to improve user experience by encouraging intuitive engagement.
Node.js modules
Node.js features multiple ‘modules’ that are kept within individual contexts so they do not interfere with other modules or pollute the global scope of node.js. This is crucial for open source softwares.
A module in Node.js is a functionality - either simple or complex - which is organized into JavaScript files and reusable throughout the Node.js application. There are three types of module within Node.js: Core Modules, Local Modules and Third Party Modules.
Core Modules include the basic, bare-bones functionalities of Node.js. They load automatically when a node process starts and are part of Node.js’s binary distribution.
Local Modules are modules that are created within the Node.js application. They include different and additional functionalities in separate files and folders to the core functionality package. Local modules can also be packaged and distributed for use by the wider Node.js community.
A third party module is an existing code written by a third party, and can be imported into your Node.js application to expand or add different features and functions.
Microservices
A microservice is an individual, self-contained unit that works alongside others to make up a large application. Splitting an app up into multiple small parts makes it easy to maintain, independently deployable and scalable. They can be written by different teams and tested individually.
Microservices are not always the best option for every project. A common issue with microservices is ‘orchestration’, the challenge of integrating the services with a drive that guides the process. ‘Discovery’ can also be a problem, where microservices struggle to locate and relate to each other on a server. These issues can be solved with a more ‘monolithic’ structure, which is a single, autonomous unit.
Both approaches have their advantages and choosing which to use depends upon the scope and requirement of the individual project.
What is event-driven programming?
Event-driven programming is a feature of applications designed to respond to various forms of user engagement. It is known as a ‘programming paradigm’, whereby the flow of the program’s execution is determined by events.
In this context, events are any action made by a user, such as clicks or key presses. They can also mean messages from other threads or programs.
Event-driven programming is designed to detect actions as they occur, and processes them using establishing event-handling procedures. This provides for a more reactive and intuitive user experience and adds flexibility to applications handling concurrent requests.
Node package manager
Node package manager (or ‘npm’ for short) does a few things; firstly, it acts as an online repository for publishing open-source Node.js projects. Secondly, it is used as a command-line facility for interacting with that repository, assisting with package installation, version management and dependency management.
It is used most commonly to publish, discover, install and develop Node programs. Essentially, it helps developers to make best use of Node.js tools and packages with a useful interface.
Node.js worker threads
Worker threads is a feature within Node.js that is useful for performing heavy JavaScript tasks. The worker_threads module enables execution of JavaScript codes in parallel, making the entire process quicker and more efficient. It does this by using an existing pool of ‘workers’ to handle new and incoming tasks.
Worker threads help with CPU-intensive tasks without disturbing the main Node.js thread, offering flexibility and compartmentalization.
Event loop
The event loop is what allows Node. js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible. Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background.
Generally speaking, the event loop is a mechanism that waits for and dispatches events or messages in a program. In Node.js, event loops are the central control flow constructs. For example, every time a request is about to be handled, it’s put on the event loop and processed as soon as it’s ready to be processed.
Node, instead of doing it on its own, delegates the responsibility of handling the system. Because of such behaviour , Node is not actively waiting for this task to finish and can handle other requests in the meantime. The event loop makes Node.js faster and more efficient than other technologies
The Popularity of Node.js
JavaScript, which underpins Node.js, has been one of the most popular programming languages available for some time. While it has more commonly been used for front-end web development, it has also gained ground in different areas of application and on distinct platforms, such as Node.js.
Even though Node.js was originally written in 2009, it wasn’t widely adopted until recently. Currently, numerous successful brands have leveraged Node.js to develop both front and back-end applications, including Walmart, Netflix, Medium, LinkedIn or Groupon.
Node.js is rising in popularity for a number of reasons, which we will go through shortly. It’s also important to know that, as with every technology, there are some drawbacks which mean Node.js isn’t perfect for every project.
What can Node.js do and what kind of products can be built with Node.js?
Node.js is a versatile JavaScript runtime environment built upon event-driven programming that enables non-blocking I/O (Input/Output) capable of serving multiple concurrent events in a single thread. This makes Node.js fast, lightweight, scalable, and efficient in handling data-heavy and I/O-heavy workloads characteristic of several types of web applications.
Internet of Things
IoT (Internet of Things) is a network of devices such as sensors, beacons, actuators, and any other items embedded with electronics that enables them to send and exchange data. IoT may consist of thousands of such devices, which makes it challenging to manage requests and data streams from and between the devices.
Since 2012, Node.js has become one of the preferred solutions for enterprises and organizations seeking to develop IoT systems because it can process multiple concurrent requests and events emitted by thousands or even millions of devices on the network without seeing a decrease in speed or performance.
The avalanche of requests and data coming from IoT devices does not block Node.js servers thanks to their event-driven architecture and asynchronous processing suitable for I/O-heavy operations on the IoT network. This makes Node.js fast as an application layer between these devices and the databases that store data from them.
Real-time chats
Real-time chat is any online communication tool that enables the live transmission of text, video, or audio messages from a sender to a receiver. They are used widely across social media apps and commercial sites and, as a result, are an important part of many industries.
Node.js provides all basic functionalities for building real-time chats of any complexity. In particular, Node has a powerful Event API that facilitates the creation of “emitters”, which periodically emit named events “listened” to by event handlers. This functionality makes it easy to implement server-side events and the push notifications widely used in instant messaging and other real-time applications.
Complex single-page applications
Single-Page Applications (SPAs) are a popular approach to web development in which an entire application fits on a single page with the goal of providing a comprehensive user experience, similar to a desktop app.
These days, SPAs are widely used to create social networking apps, online drawing and text tools, and many more. The classic example of an SPA is Gmail, with its cohesive presentation and seamless updates of new incoming messages.
Node.js is a great fit for SPAs thanks to its ability to handle the asynchronous calls and data-heavy workloads characteristic of these applications. Node.js’s event loop can “delay” multiple concurrent requests from a client, which ensures smooth transitions between views and seamless data updates.
Finally, Node.js is good for SPAs because it is written in the same language (JavaScript) as many popular JavaScript frameworks (Ember, Meteor, React, Angular) used in building SPAs.
Since both Node.js and browsers use JavaScript, there is less context switching between them, and developers can use the same data and language structures and modular approaches both on the server and the client side. This results in faster development and better maintainability of your SPAs.
For this reason, Node.js is popular with several social sites, like LinkedIn and Medium.
Real-time collaboration tools
Real-time collaboration applications offer a variety of software solutions for co-browsing, project management, video and audio conferencing, application sharing, collaborative editing of documents, and more. Popular and successful examples include Slack, Trello and Google Docs.
As in the case of the real-time chats, Node’s asynchronous and event-based architecture is a great fit for collaboration apps. In these applications, many events and requests occur concurrently. For example, several users can edit the same paragraph, comment, post messages, and attach media from different devices. Changes to one piece of content might be applied only after a cascade of events, where each step depends on the previous one.
By emitting push notifications to the client, Node.js will also instantly update the collaboration environment so that all users have a single and coherent representation of the application. This is precisely the reason why the team of the project management application Trello uses the Node.js stack. The engineering team of Trello decided that Node.js would be great to instantly propagate multiple updates and hold a lot of open connections, thanks to its event-driven and non-blocking architecture.
Streaming apps
Application streaming allows parts of the application to be downloadable on demand without overloading the server and the local computer. Initially, only certain parts of the application needed for bootstrap are downloaded. The remainder can be downloaded in the background if necessary. When the application is completely downloaded, it can function without any network connection at all.
Node.js is excellent for the development of streaming applications thanks to its native Stream API. It has an interface of readable and writable streams that can be processed and monitored very efficiently. Stream instances are basically Unix pipes that allow parts of the app’s executable code to be transmitted to the local machine, keeping a connection open for new components to download on demand.
As a bonus, streams do not require caching and temporary data – just an open connection to stream application data from one place to another.
Microservices architecture
In recent years, microservices architecture has become extremely popular with many major applications such as Netflix, Facebook, Amazon, and eBay, which have evolved from monolithic applications to a suite of microservices.
Node.js is an excellent solution for developing microservices and creating easy-to-use APIs to connect them. In particular, the Node.js repository features Express and Koa frameworks, which make it easy to mount several server instances for each microservice and design routing addresses for them.
These benefits have been successfully leveraged by PayPal, which has used Node.js to power its microservices architecture since 2013.
Why Should I use Node.js?
Some of the benefits of Node.js are as follows:
1. Easy to Learn
Javascript is one of the most popular programming languages for front-end development, and nearly every front-end developer is familiar with this universal language.
Therefore, it is much easier for developers to switch to using Node.js at the back-end. It requires less effort and less time to learn and work with, even for a junior Javascript programmer.
2. Freedom in app development
While Ruby on Rails is a framework that imposes rules and guidelines for developing software in a particular way, Node.js offers more space and freedom for doing things your own way (see below for a more in-depth comparison between Node and Ruby).
Node.js is completely un-opinionated, meaning you can build everything from scratch and tailor every last detail. It can execute basic tasks, but gives you only the bare minimum from a fresh install, allowing you to add features from there with less restrictions.
3. Fullstack JS
Before Node.js, JavaScript was only used for client-side development. It was necessary to use a different server-side programming language. In practice, you had to hire separate development teams for backend and frontend.
With Node.js growing in popularity, fullstack JavaScript became a reality. Nowadays it is possible to write both the front-end and back-end of web applications in Javascript, making app deployment much easier and more efficient.
4. Active community
The Node.js developers community is an active and vibrant group of developers who contribute to constant improvement of Node.js. Thanks to the cooperation of JavaScript programmers and their input to the community you get access to a ton of ready solutions, codes in Github and many more possibilities.
Even though it is still at a relatively early stage of development, the community is evolving dynamically and its members go the extra mile to provide others with best-in-class, reliable solutions.
5. Simultaneous Request Handling
Node.js provides a non-blocking I/O system that lets you process numerous requests concurrently. Incoming requests are queued up and executed sequentially in rapid time. In effect, your app will take up much less system RAM, achieve high scalability levels and will perform faster as a result.
Disadvantages of Node.js.
Even with all of the benefits listed above, there are some ‘cons’ that come with Node.js that mean it might not be the right tool for all application development projects. Here’s a few of the disadvantages of Node.js.
1. Unstable API
One of the biggest disadvantages of Node.js is that it lacks consistency. Node.js’s API changes frequently, and the changes are often backward-incompatible.
When this happens, programmers are forced to make changes to the existing code base to make it compatible with the latest version of the Node.js API. Obviously this can create extra work that distracts from the goal of improving your application.
2. More development time
The fact that Node.js is un-opinionated can also be seen as a drawback by some developers. Ruby on Rails provides you with a lot of directions from a fresh install and guides you into their way of doing things, but with Node.js you basically need to write everything from scratch.
This can offer flexibility, but it can also result in a decrease in productivity, slowing your work down. However, if you cooperate with an experienced team of programmers who have internally developed good processes for developing and maintaining code, you won’t have to worry about this.
3. Unsuitable for heavy-computing apps
Node.js doesn’t support multi-threaded programming yet. It is able to serve way more complicated applications than Ruby, but it’s not suitable for performing long-running calculations. Heavy computations block the incoming requests, which can lead to decrease of performance. While Node.js is perfect for complex apps, in the case of software which requires some heavy-computing it might perform less effectively.
4. Immaturity of tools
Even though the core of Node.js is stable, many packages in the Node package manager (npm) registry are still of poor quality or have not been properly documented. As it is mostly an open source ecosystem, some tools have not been supervised and they might lack quality, failing to meet coding standards. The npm’s structure makes it harder to spot reliable packages. Therefore, you might need more experienced developers who can find tools that can be trusted.