Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am a n00b in this kind of stuff but lately I've been hearing a lot about how good NodeJS is. Considering how much I love working with jQuery and Javascript in general, I can't help but wonder how to decide when to use NodeJS. The web application I have in mind is something like bit.ly - takes some content, archives it.

From all the homework I have been doing in the last few days, I obtained the following information. NodeJS

  • is a command-line tool that can be run as a regular web server and lets one run Javascript programs
  • utilizes the great V8 JS engine
  • is very good when you need to do several things at the same time
  • is event-based so all the wonderful Ajax like stuff can be done on the server side
  • lets us share code between the browser and the backend
  • lets us talk with MySQL

Some of the sources that I have come across are:

Considering that NodeJS can be run almost out-of-the-box on Amazon's EC2 instances, I am trying to understand what type of problems require NodeJS as opposed to any of the mighty kings out there like php, python and ruby. I understand that it really depends on the expertise one has on a language but my question falls more into the general category of: When to use a particular framework and what type of problems is it particularly suited for?

Any suggestions?

share|improve this question
72  
+1 for asking a superb question – jsalonen Apr 9 '12 at 7:46
Very nice. Here is a similar question I've had to pleasure to answer on programmers.stackexchange on whether or not to use node for a financial application programmers.stackexchange.com/questions/184912/… – Benjamin Gruenbaum Jun 7 at 0:22

7 Answers

up vote 308 down vote accepted

You did a great job of summarizing what's awesome about node. My feeling is that node 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", you can write an application that sends updates to the user in real time. Doing long polling on many of the web's giants, like 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 you use something like node, the server has no need of maintaining separate threads for each open connection.

This means you can create a browser-based chat app in node that takes almost no system resources to serve a great many clients. Any time you want to do this sort of long-polling, node is a great option.

It's worth mentioning that ruby and python both have tools to do this sort of thing (eventmachine and twisted, respectively), but that node does it exceptionally well, and from the ground up. JavaScript is exceptionally well situated to a callback-based concurrency model, and it excels here. Also, being able to serialize and deserialize with json native to both the client and the server is pretty nifty.

I look forward to reading other answers here, this is a fantastic question.

Edit: It's worth pointing out that node is also great for situations in which you'll be reusing a lot of code across the client/server gap. The (currently brand new) meteor framework makes this really easy, and a lot of folks are suggesting this might be the future of web development. I can say from experience that it's a whole lot of fun to write code in meteor, and a big part of this is spending less time thinking about how you're going to restructure your data so the code that runs in the browser can easily manipulate it and pass it back.

Edit: Here's an article on Pyramid and long-polling, which turns out to be very easy to set up with a little help from gevent: TicTacToe and Long Polling with Pyramid

share|improve this answer
11  
+1 You make some very important points about long-polling and chat-based application. And of course, it makes perfect sense to use Javascript's concurrency model. Thank you. – Legend Feb 22 '11 at 17:05
3  
This has also been my answer when asked. It's the easiest example to draw upon. I don't have enough hands on experience, so I'm speculating, but I suspect other good uses: static file server or CDN node, video/audio streaming, web service glue. I think even these examples are uninspired. A Rack server optimized for developer workflow was written in Node (pow.cx), e.g. a server of Ruby web apps/services. I think the real awesomeness will arrive from someplace unexpected. – toolbear Jul 25 '11 at 13:51
1  
Yes, I think it is very important to think that 'node.js is especially suited for applications that require persistent connection from the browser back to the server. - such as chat programs, or interactive games' If one is just building an application that does not necessarily need user/server communication, developing with other frameworks would be just fine and will take much less time. – user482594 Sep 27 '11 at 7:03
2  
This answer is somewhat outdated. There are better solutions for real-time client<->server messaging, and the crown jewel is Socket.IO, a famous module for node.js which allows easy real-time communication via web sockets, flash sockets or - perhaps - polling (whatever's available on target browser). – Kos Jun 9 '12 at 10:48
6  
I'd argue that the if Socket.IO, a node module, is the crown jewel of real-time communication, then Node is still the best solution for real-time client<->server messaging. – Benson Jun 12 '12 at 17:31
show 4 more comments

I 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.

I should also mention that Socket.IO in combination with Node.js will reduce your 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 I 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 you need the application to be event-driven.

Also, Ryan Dahl said in a talk that I once attended that the Node.js benchmarks closely rival Nginx for regular old HTTP requests. So if we build with Node.js, we can serve our normal resources quite effectively, and when we need the event-driven stuff, it's ready to handle it.

Plus it's all JavaScript all the time. Lingua Franca on the whole stack.

share|improve this answer
2  
+1 Thank you for the detailed post. – Legend Feb 22 '11 at 17:06

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.

Here is a good article about the event loop in node.js: Mixu's tech blog: Understanding the node.js event loop

share|improve this answer
3  
Nice and sweet answer. – shamittomar Dec 13 '12 at 15:09

I have one real world example where I have used node.js. The company where I 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. I decided to make the counter using node.js and expressjs framework.

The node.js application was simple. Get the sold items amount from redis database, increase the counter when item is sold and serve the counter value to users via api.

Some reasons why I chose to use node.js in this case

  1. It is very lightweight and fast. There has been over 200000 visits on this website in 3 weeks and minimal server resources has been able to handle it all.
  2. The counter is really easy to make to be real time.
  3. Node.js was easy to configure.
  4. There are lots of modules available for free. For example I found a node.js module for Paypal.

In this case node.js was an awesome choice.

share|improve this answer
Great real world example! Thanks! – Fillip Peyton Jun 7 at 19:40

There is nothing like Silver Bullet. Everything comes with some cost associated with it. It is like if you eat oily food, you will compromise your health and healthy food does not come with spices like oily food. It is individual choice whether they want health or spices as in their food. Same way Node.js consider to be used in specific scenario. If your app does not fit into that scenario you should not consider it for your app development. I am just putting my thought on the same:

When to use Node.JS

  1. If your server side code requires very less cpu cycle. In other world you are doing non blocking operation and does not have heavy algorithm/Job which consumes lots of CPU cycle.
  2. If you are from Java Script back ground and comfortable in writing Single Threaded code just like client side JS.

When NOT to use Node.JS

  1. Your server request is dependent on heavy CPU consuming algorithm/Job.

Scalability Consideration with Node.JS

  1. Node.JS itself does not utilize all core of underlying system and it is single threaded by default, you have to write logic by your own to utilize multi core processor and make it multi threaded.

Node.JS Alternatives

There are other option to use in place of Node.JS however Vert.x seems to be pretty promising and has lots of additional features like polygot and better scalability considerations.

share|improve this answer
4  
I am not sure about "If your server side request includes blocking ops like File IO or Socket IO" being listed in "When NOT to use". If my understanding is right, one of the strengths of the node.js is, that it has powerfull asynchrounous means to handle IO without blocking. So Node.js can be viewed as "a cure" for blocking IO. – Ondra Peterka May 24 at 13:00
@OndraPeterka: You are right that Node.js is cure to blocking server IO, however if your request handler on the server it-self is making a blocking call to some other web service/File operation, Node.js would not help here. It is non blocking IO only for incoming requests to server but not for outgoing request from your app request handler. – ajay Jun 5 at 7:28
1  
@ajay from nodejs.org they say "non-blocking I/O", please check your "When NOT" 2 and 3. – OmarIthawi Jun 6 at 8:23

My piece: nodejs is great for making real time systems like analytics, chat-apps, apis, ad servers, etc. Hell, I made my first chat app using nodejs and socket.io under 2 hours and that too during exam week!

share|improve this answer

Another great thing that I 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 you can include by simply including them in your package.json file.

share|improve this answer

protected by Community Mar 20 '12 at 20:22

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.