NodeJS – Beginners Guide to Understanding NodeJS
If you have heard about Node, or if you have read an article highlighting how amazing it is, you may be wondering “Simply what is Node.js?”. Node is definitely not for all programmers, but it could be the right answer for some.
This article will try to respond to what is Node.js, presenting a brief context of the problems it solves, how it works, how to run a simple application and, finally, where Node is a good solution.
What problem does NodeJS solve?
Node’s declared number one goal is “to provide an easy way to build scalable network programs.”
So, what is the problem with current server programs? Let’s explore this. In languages such as Java and PHP, each connection generates a new thread that is potentially accompanied by 2 MB of memory. In a system that has 8 GB of RAM, this gives a theoretical maximum number of concurrent connections of about 4,000 users. As your customer base grows, if you want your application to support more users, you will need to add more and more servers.
Of course, this adds to the server’s business costs, traffic costs, labor costs, and more. In addition to these costs are the costs for potential technical problems – a user may be using different servers for each request, so any shared resource must be stored on all servers.
For all these reasons, the bottleneck in the entire Web application architecture (including traffic performance, processor speed and memory speed) was the maximum number of concurrent connections a server could handle.
NodeJS solves this problem by changing the way a connection to the server is made. Instead of generating a new OS thread for each connection (and allocating the companion memory), each connection triggers an event execution within the Node engine process. Node also states that it will never remain in neutral, because blockages are not allowed and because it is not blocked directly for E/S calls. Node states that a server running it can support tens of thousands of concurrent connections.
So, now that you have a program that can handle hundreds of thousands of concurrent connections, what can you actually build with Node? It would be extraordinary if you had a Web application that needed all this number of connections. That is one of those difficulties of the type “if you have this problem, it is not a problem”.
Before moving on to this, let’s look at how Node works and how it is designed to run.
What NodeJS is definitely not
Yes, NodeJS is a server program. However, Node’s base product is definitely not like Apache or Tomcat. These servers are basically server products available to install and ready to deploy applications immediately. You could have a server be ready and in operation in a minute with these products. Node is definitely not this.
Similar to how Apache can add a PHP module to allow developers to create dynamic Web pages, and an SSL module for secure connections, Node also has the concept of modules that can be added to its core itself.
There are literally hundreds of modules from which you can choose with Node, and the community is quite active in producing, publishing and updating dozens of modules per day. We will talk about the whole part of Node modules later in this article.
How Node works
Perfect! Why do you need to create a new language when there is already a good solution available.
Node, what is it for?
Then, after reading all about Node, you may be able to answer the question “Simply what is Node?” but you may have the question about “What can I use Node?” That is an important question since there are some things for which Node is really good.
As you have seen so far, Node is extremely well designed for situations where you are waiting for a large amount of traffic and where the required server-side logic and processing are not necessarily large before responding to the client.
Here are some good examples where Node would do a great job:
A RESTful API
A Web service that provides a RESTful API takes some parameters, interprets them, assembles a response and downloads that response (usually a relatively small amount of text) back to the user.
This is an ideal situation for Node because it can be built to handle tens of thousands of connections. It also does not require a large amount of logic and basically only looks for values from a database and gathers them as an answer. As the answer is a small amount of text and the incoming request is a small amount of text, the volume of traffic is not high, and a machine can probably handle the API demands of even the busiest API of the companies.
Think of a company like Twitter that receives tweets and writes them to a database. There are actually thousands of tweets appearing every second and the database may not be able to keep up with the amount of writes needed during peak usage hours.
Node becomes a key piece of the resolution to this difficulty. As we have seen, Node can handle thousands of incoming tweets. You can then write them quickly and easily in a memory queue mechanism (memcached, for example), from where another separate process can write them to the database.
Node’s role in this is to quickly gather the tweet and pass this information to another process responsible for writing it. Imagine another design – a normal PHP server that attempts to handle writes to the database itself – each tweet could cause a little delay while writing to the database since the database call would be blocking.
A machine with this design could only handle 2,000 incoming tweets per second, due to database latency. At one million tweets per second, you would be talking about 500 servers. Node, on the other hand, handles each connection and does not cause blocking, allowing you to capture as many tweets as you can throw. A node machine capable of handling 50,000 tweets per second, and you would be talking about only 20 servers.
Video game statistics
If you ever played a game like Call of Duty online, some things will have caught your attention immediately when you looked at the game’s statistics, mainly the fact that they must be tracking tons of information about the game in order to produce such a level of statistics. Then, multiply this by the millions of people who play it at any time, and you will have an idea of the immense amount of information that is generated quite quickly.
Node is a good solution for this scenario because you can capture the data that the games are generating, make a minimum of consolidation with them and then put them in a row to write them in a database.
It would seem silly to dedicate a whole server to track how many bullets people fire in games, which could be the useful limit if you used a server like Apache, but it would seem less silly if instead, you could dedicate a single server to track Almost all the statistics of a game, as you can get to do it with a server running Node.
The significance of events in NodeJS
Its architecture is built on events and because of this, Node can generate a variety of asynchronous processing of output and input actions. NodeJS is built on a single process thread able to contain all the events that happen.
When there is an event, your system is ready to run with them at various levels by its callback function. In this case, various processes will not be produced at an equal level. All events are components of the same process thread, they will operate with them within parallel functions through the equal loop. When a background task is completed, the callback function solves the delivery of the event and can be passed to the customer in a very short period.
Node Package Manager
One of the advantages of Node is its Package Manager. A sort of manager that provides access to a very extensive collection of libraries that are also free and made from the collaboration of users in their community. Some of the most popular sets that you can find on their platform are Gulp, React, Grunt-CLI, Browserify, Grunt, Forever, Cordova, Express or Bower.
Advantages of NodeJS
– It is a language that can be applied to a large number of server types, including the best known as Mac, Microsoft, and Unix.
– A vast majority of the most valuable and well-optimised platforms, such as PayPal and Linkedin, was developed from NodeJS. And that is the most reliable sign of quality and possibilities.
– It provides high performance. Not only can it generate stable and powerful architectures, but it also significantly decreases the error frequency.
– Its high scalability has taken the community of programmers to a new level. Now developers can create strong applications with a wondrous scalable potential. But what does this transpose into? In web applications with unusual possibilities as semantics that let hundreds and hundreds of offers to a single server at the same time. This has enabled increasing the abilities of web applications that until then could carry up to a peak of three thousand clients per server. Though, NodeJS has provided increasing the potential to host active users in applications and without increasing the server number.
– It is possibly the most ambitious decision to produce applications that run huge amounts of data created by a great user community. A great sample would be also Facebook, a platform that produces thousands of content for every second.
– Because of the fact that NodeJS provides operating both from the server and from the customer, it is achievable to create a much more agile and instant transference of data. The end of all this is a significant decrease in work times.
– Because of its high performance to handle and process huge contemporary amounts of data, Node is a star choice for the development of applications such as interactive games or online chats.
– It is created to support the dialogue between clients and developers: It has grown a wide community that provides discovering very unusual collaboration ties from various aspects which is very enriching for programmers. Besides being a well-known language and used by experts around the world it is easy to find information and support online.
Disadvantages of NodeJS
Unstable API. The Node API has a bad habit of changing in ways that break backward compatibility from version to version, which requires you to apply frequent changes to your code to keep everything running in the most current versions.
Many ways to program. The inherent lack of code organisation can be considered a major disadvantage. Its effect is clearly noted when the development team is not very familiar with asynchronous programming or standard design patterns. There are simply too many ways to program and to obtain uneven code and difficult to maintain.
Like all open-source projects, Node.js will continue to evolve and developers will discover new resources and techniques to overcome any number of limitations.
As crucial as learning what Node is, it is also necessary to learn what Node is not. Node is not just an Apache replacement that will immediately make your PHP Web application more scalable. That could not be further from the truth. It is still early in Node’s life, but it is growing extremely fast, the community is active, a large number of modules are being produced, and this growing product could be crucial to the development of your website and web applications
February 24, 2020