50

Recently there has been a lot of praise for Node.js. I am not a developer that has had much exposure to network application. From my bare understanding of Nodes.js, its strength is: we have only one thread handling multiple connections, providing an event-based architecture.

However, for example in Java, I can create only one thread using NIO/AIO (which is non-blocking APIs from my bare understanding), and handle multiple connections using that thread, and I provide an event-based architecture to implement the data handling logic (shouldn't be that difficult by providing some callback etc.)?

Given JVM being a even more mature VM than V8 (I expect it to run faster too), and event-based handling architecture seems to be something not difficult to create, I am not sure why Node.js is attracting so much attention. Did I miss some important points?

7
  • 3
    I wonder why it is downvoted... I feel this question is more a programming discussion so I didn't put in stackoverflow. I also tried to search similar topics but I only found information about the strength of Nodes.js but my problem is I don't really understand why the "strength" is so unique (which I still can't find ) Jun 19, 2012 at 2:42
  • 6
    Try implementing that pattern in Java. It works, of course. But you'll see one thing: It'll be very, very verbose in Java: you need tons of callbacks, which almost always means creating new classes. That might sound like a minor nitpick, but in a big program it can get ugly and unwieldly quite quickly. Jun 19, 2012 at 5:20
  • 6
    Javascript callbacks are just as prone to becoming unwieldy -- and such spaghetti is far easier to debug and refactor in Java than in Javascript IMO.
    – funkybro
    Jun 19, 2012 at 8:00
  • 5
    @AdrianShum: its a slashdot effect, anything that doesn't fit in with the group mindset will be downvoted - like praising Microsoft on /.
    – gbjbaanb
    Jun 19, 2012 at 8:54
  • 3
    I'm surprised to not see anyone mentioning the hype.
    – deadalnix
    Jun 19, 2012 at 8:59

3 Answers 3

34

While that concept can indeed be implemented in many languages (and as dodgy_coder mentioned, it has been implemented in Ruby and Python at least), it's not quite as trivial as you state.

True, Java has non-blocking IO APIs. So you can do raw disk/network IO in a non-blocking way. However every API that somehow wraps or handles IO needs to be implemented in a non-blocking way as well. Every XML parser, every Database driver, every file-format converter needs to be written to support non-blocking IO. Because if a single library is blocking in this pattern, then that brings down your servers performance to stone-age values.

Node.js has that library infrastructure, because it was always designed that way: every library that strives to become popular has to provide an asynchronous API or it will not be used.

4
  • 19
    Yep. In other words: Node.js's most important strength is ECMAScript's most important weakness: the incredibly crappy ECMAScript standard library. Since the Node.js developers had to reinvent every single wheel anyway, they had the chance to reinvent it the right way. Jun 19, 2012 at 7:59
  • 4
    Well, as far as I know ECMAScript was always designed as an embedded language, so there was no need for OS-level APIs of any kind (even network IO was mostly abstracted away). That lack was indeed an advantage to Node.js. Jun 19, 2012 at 8:07
  • "every library that strives to become popular has to provide an asnychronous API or it will not be used" I think that's precisely what I am looking for. Is there any resource to look into about how asynchronous api is provided for, for example, XML parsing and DB access, in Node.js? Jun 19, 2012 at 9:24
  • 1
    @AdrianShum in general, look for event driven programming examples. Specific implementations can be found in many languages. Besides Node.js modules, you could look at Twisted examples in Python, twistedmatrix.com/trac/wiki, POE examples in Perl, poe.perl.org, and Ruby has EventMachine, github.com/eventmachine/eventmachine
    – mghicks
    Dec 20, 2012 at 11:56
19

Probably the main reason is that it uses JavaScript to write server-side components for things such as web servers, web applications or web services. This unifies the traditional front end (client-side) development language JavaScript with the server-side language.

You're right - the fact it is non-blocking, using the reactor pattern is not unique - it has been done before using other languages and frameworks, such as Ruby's EventMachine or Python's Twisted for example.

2
  • 5
    very few technologies have truly unique features, the uniqueness comes from their particular combination of all their features
    – jk.
    Jun 19, 2012 at 7:30
  • 1
    Agreed, there's a list here of libraries which support it in all major languages ... Reactor Pattern on Wikipedia Jun 19, 2012 at 13:37
10

The three main reasons I would give are:

  1. Non Blocking IO / Asynchronous IO. This is hashed everywhere on the web and in the previous posters. One thing I would contribute is that designing your code to explicitly assume asynchronous behaviours assists the compiler engine to maximize the hardware. Yes, many of the JIT compilers and hyperthreading processors take synchronous code and help parallelize the execution. This of course is a best effort approach. In contrast, explicitly building the application for async io you ensure the engine and the hardware can maximize the execution time for your code. Admittedly, I don't have quantifiable data to prove this, but it makes me feel warm inside to think this way.

  2. Single code base for client and server. This has a number of advantages: help optimize datacenter costs by being able to move business logic from the server to the client; help reuse business logic / data validation; reduce the complexity of developer skills that need to exist to support the product (vs. Python & javascript).

  3. Low barrier to entry. In many ways Javascirpt is like Basic, Pascal and Perl of yester-year. It is super easy to start writing code and does not require a lot of domain knowledge to get going. This also helps reduce your development costs by being able to bring in more jr developers and getting the ramped up on a project. [Of course you need to get past the ideologues who believe programming languages should be difficult in order to weed out the low functioning developers]

2
  • I'm not sure I'd recommend building a Node team entirely with jr. JS devs. Architecture isn't really something we have to think about in more jr-grade web/UI projects and JS takes as long to get really good at in terms of building for the long haul as any other language does even if you can get results relatively fast at a lower level of experience on less complex projects than is typical. Aug 20, 2014 at 21:25
  • I agree with @ErikReppen; an architecture team entirely of junior devs (regardless of language) is like designing and building a house entirely using carpenters who are good at building chairs, tables and dog houses.
    – Wildcard
    Apr 18, 2016 at 20:04

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