How to decide when to use node.js ?
We did a great job of summarizing what's awesome about Node.js. My feeling is that Node.js is especially suited for applications where you'd like to maintain a persistent connection from the browser back to the server. Using a technique known as "long-polling" , we can write an application that sends updates to the user in real time. Doing long polling on many of the web's giants, like Ruby on Rails or Django , would create immense load on the server, because each active client eats up one server process. This situation amounts to a tarpit attack. When we use something like Node.js, the server has no need of maintaining separate threads for each open connection.
- We believe Node.js is best suited for real-time applications: online games, collaboration tools, chat rooms, or anything where what one user (or robot? or sensor?) does with the application needs to be seen by other users immediately, without a page refresh.
- WE should also mention that Socket.IO in combination with Node.js will reduce our real-time latency even further than what is possible with long polling. Socket.IO will fall back to long polling as a worst case scenario, and instead use web sockets or even Flash if they are available.
- But WE should also mention that just about any situation where the code might block due to threads can be better addressed with Node.js. Or any situation where we need the application to be event-driven.
- To make it short:
- Node.js is well suited for applications that have a lot of concurrent connections and each request only needs very few CPU cycles, because the event loop (with all the other clients) is blocked during execution of a function.
- WE have one real-world example where WE have used Node.js. The company where WE work got one client who wanted to have a simple static HTML website. This website is for selling one item using PayPal and the client also wanted to have a counter which shows the amount of sold items. Client expected to have huge amount of visitors to this website. We decided to make the counter using Node.js and the Express.js framework.
- The Node.js application was simple. Get the sold items amount from a Redis database, increase the counter when item is sold and serve the counter value to users via the API.
Some reasons why we chose to use Node.js in this case
- It is very lightweight and fast. There has been over 200000 visits on this website in three weeks and minimal server resources has been able to handle it all.
- The counter is really easy to make to be real time.
- Node.js was easy to configure.
- There are lots of modules available for free. For example, WE found a Node.js module for PayPal.
- In this case, Node.js was an awesome choice
Another great thing that we think no one has mentioned about Node.js is the amazing community, the package management system (npm) and the amount of modules that exist that we can include by simply including them in our package.json file.