Before getting into this article, I want to convey that all the articles posted in C# Corner are completely (in my opinion) based on what I have learned about technology. So that applies even to this article.
In my previous parts of this article series, we discussed the main components and internal architecture of EventLoop. Today, we will discuss when to use NodeJS, in other words how to choose NodeJS as the server-side technology for your application.
According to nodejs.org, NodeJS apps are fast and scalable because NodeJS uses a non-blocking IO model and is event-driven.
An application created using NodeJS will be fast and scalable only when it is developed with some conditions in mind.
When to Use NodeJS
Though NodeJS gives you fast and scalable apps, you can't simply use that for all the apps you want. You can use NodeJS for the apps like chatting app, dashboard apps and streaming apps and so on.
Realtime apps
The chat application is a typical real-time, multi-user application example for NodeJS. Because it's a lightweight, high traffic, data-intensive but low processing/computation application that runs across distributed devices.
The NodeJS module for socketio.js is the best module that can be used to create the real-time chat apps and monitoring apps.
Data Streaming apps
We can create streaming apps like real-time video, audio encoding, to process the files while they're still being uploaded. Since the HTTP requests and responses are streams, it easy to use NodeJS for creating applications to handle the stream manipulations.
Web API with JSON
With NodeJS, we can simply expose your JSON objects with a REST API for the client to consume. Here the data stores can be NoSQL/document DBs like MongoDB, CouchDB, and so on.
And you don't need to worry about converting between JSON and native models since we are using a JSON data store (MongoDb…).
NodeJS apps as Web Proxies
NodeJS can be used to create web proxies and load balancers to mirror the incoming requests to their corresponding path on the target site. Since NodeJS can handle the multiple requests and serve thousands of concurrent users it is best suited to create proxies.
The Node module "node-http-proxy" can be used for such kind of web proxies and load balancers.
When NOT to use NodeJS
Since we have some criteria to choose NodeJS as a development platform, we even have reasons to not use NodeJS.
NodeJS is not the correct choice if your application belongs to the following category.
Node Apps with Relational DBs
We already discussed that using JSON data stores like MongoDb is the best option to create a Web API. Why? Because the JSON store is simple and fast compared with the other Relational DBs.
The comparison chart here shows that the no-SQL db is faster and scalable. Since the NodeJS uses a non-blocking IO and event-driven approach, it is better to choose the DBs also faster and scalable one.
Heavy Processing Apps
The nature of NodeJS apps will be broken if we develop an app that does heavy CPU intensive work. If your application needs to do some data intensive work, then the responsiveness of NodeJS could be dropped out.
Since the NodeJS environment is single-threaded, if you have IO blocking code in your application, even if the app does not respond for 2 seconds then the concurrent request will put the server down.
Conclusion
As a conclusion, it's better to use NodeJS for simple real-time apps something like Chat apps, system monitoring apps, dashboard apps and games like online chess.
If you have any question or suggestion on content, please do comment.
Thanks for Reading.
Happy Coding