Unless you have been living under rock, chances are that you would have heard a lot about how Node.js has revolutionized web development. Node.js has been gaining a lot of popularity these days as a suitable replacement for traditional frameworks that run on web servers.
Many Enterprises seem to have embraced it readily portals like Yahoo, Social platforms like LinkedIn, streaming media providers like Netflix, hosting companies like GoDaddy, even NASA is using it in it's space-suits. According to a recent Stackoverflow developer survey, it is one of the most popular tools today among professional developers.
So what is Node.js exactly?
What does "event-driven non-blocking I/O model" really mean in layman's terms?
Print(100); Print(101); Print(102);
The output will be 100, 101, 102 in that order because unless one instruction is completed the other doesn't start.
Print(100); DoLater().then( () => Print(101) ); Print(102);
The output will be 100, 102, 101 in that order because 101 is processed asynchronously, so the 3rd instruction doesn't wait for the completion of the 2nd one. So to summarize non-blocking is when the execution of code in the doesn't wait until a specific operation is completed, this is perhaps the biggest advantage that Node.js offers.
Let's take a closer look at Node.js and figure out the pros and cons of it and decide when it should be used and when it shouldn't.
What works amazingly for Node.js
With Node.js, scalability is by design from. It can handle 10s of 1000s of concurrent requests thanks to it's non-blocking I/O structure. It can process huge amounts of requests without waiting for results. Since Node.js doesn't spawn a thread for every new request, it doesn't need to handle pooling of threads like other servers such as IIS or Apache do. All the APIS in Node support callbacks, this allows you to write non-blocking code that performs very efficiently.
3) Real time applications:
Node.js really outperforms conventional web servers when it comes to real-time messaging applications like chat etc. Since it can handle large no of client requests simultaneously and share data synchronized data on the server, constructing applications that require rapid two-way communication between the client and the server becomes very simple. Sockets.IO module for Node greatly simplifies development of applications that rely on web sockets.
4) Data Streaming applications:
Node.js was created after Ryan Dahl saw a file upload progress bar on Flickr. The browser wasn't aware of how much of the file had been uploaded and he had to query the Web server, he created Node in quest of an easier way to do this. Node's excellent inbuilt support for reading and writing streams allows you to process the streams at the same time as uploading. This makes it suitable for applications that require real-time video or audio streaming.
5) Huge Package repository:
NPM the default package manager for Node.js is also the world’s largest software registry. Given its popularity among the developer community and the enterprise world, it comes as no surprise that a large no of modules is freely available to developers worldwide. There are plenty of popular frameworks like Express that are available in Node.js that provide you with a robust set of features to develop Mobile and Web applications.
Downsides of using Node.js
1) Not suitable for connecting to relational databases.
Node.js might not be the best fit when it comes to working with relational databases. It might be a bit more challenging for you to get good driver support for your favourite database like SQL Server. The reason for this could be that since Node is almost entirely community driven, you have a lot more focus on using Node’s connectivity with NO-SQL systems like Mongo than databases. .NET or Java frameworks score much better in this area as compared to Node.
2) Blocking I/O issues.
3) Not suitable for heavy computations.
4) Callback becomes complicated.
In Node.js the code you write is going to be executed in a single thread, while performing multiple operations that require significant computation you would need to employ callbacks, now the problem here is that if there are multiple such functions then it can quickly lead to slippery slope and your code might become pretty much convoluted at the end. This is also not very intuitive and harder for some developers to follow.
Node.js is an incredibly powerful and efficient server-side technology that can help develop responsive web applications rapidly. It is here to stay and can greatly increase your team productivity as long as you are aware of certain design limitations. It might not be the best server-side system out there but it certainly offers you scalable and flexible methods for you to develop applications!.