Node.js is an open source and cross platform Java-script runtime environment that runs on a JavaScript engine and executes Java code.
Node.js allows developers to write command line tools and do server scripting to make dynamic web content before the page is sent to the user´s browser.
In essence, it´s a platform that is very useful for web development. But it´s a bit complex for newcomers and can be inefficient if used in determined aspects in web development. Think of it as a tool that is very useful in certain niches of the web development process. Here we´ll get into how it works and where it´s best to use.
How does it work?
Node.js operates on a single thread and uses non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections held in the event loop. This contrasts with traditional web-serving techniques, where each connection spawns a new thread, using up RAM and eventually maxing-out at the amount of RAM available.
For you to better understand this process, let´s see how Node.js processes connections step by step:
- The program maintains a limited number of threads to serve the request
- When a request comes, Node.js puts it on queue
- Then the single thread loop comes to action, waiting indefinitely for requests
- When the request is received, the loop picks it up and checks to see If it requires a blocking input/output (I/O) operation
- If it has a blocking request, it assigns a thread from the limited amount the program has
- After the blocking task is completed, the event loop monitors blocking requests and adds them to the queue. That’s how it keeps its non-blocking nature.
Because Node.js uses a limited amount of threads, it uses less memory which makes for a faster task execution. This makes the use of Node.js perfect for real-time applications.
Where should you use Node.js?
Now that you know what Node.js and how it works, here is the best places to use it:
Chat
The chat application is truly the ideal use case for Node.js because it is a small, widely distributed, data-intensive (but low processing/computation) application with significant traffic. As it’s straightforward yet covers most of the paradigms you’ll ever use in a regular Node.js application, it’s also a fantastic use-case for learning.
Data streaming
HTTP requests and responses on most web platforms are data streams. This allows you to use Node.js to implement features as file processing while they´re downloading. Which can be useful for real time audio and video encoding, also for proxying between data sources
Application and system monitoring
A useful thing you can do with Node.js is a real time monitoring dashboard for your website. The program is a perfect fit for tracking visitors and seeing their interactions in real time.
By adding targeted interactions with your visitors and opening a communication channel when they reach a certain stage of your funnel, you can collect real-time statistics from your users.
With Node.js you can also setup a dashboard for your clients to have a monitoring page, were the program creates a loop to check the services´ status and push the data to clients.
These statuses can be reported in real time and create the possibility of creating a Network operations centre.
Where you don´t want to use Node.js
As we´ve seen, the platform is very useful for processing information in real time to create dashboards or stream files. Here´s a few instances were using Node.js is a bad idea:
Heavy server processing or computing
Node.js is not the ideal program for doing heavy computing or data processing tasks. Intensive CPU operations null the event driven non-blocking I/O model that the platform is characterised by.
When you execute heavy tasks on Node.js, the requests will be blocked. Whilst the thread is occupied with number crunching.
Conclusion.
Node.js is a very useful platform for web developing and management. But it´s use must be specific to the programs characteristics . If you have to do heavy computing that requires more than one core, then you shouldn´t use it.
However if your work doesn´t require CPU intensive operations or blocking resources, you can use it to have fast and scalable network applications in real-time.
Here at Obambu, we allow the use of Node.js for web development on any of our hosting plans and VPS.