views:

1983

answers:

4
+18  Q: 

What is node.js?

I don't fully get what node.js is all about. Maybe it's because I am mainly a web based business app developer. Can someone please explain what it is and the use of it? Thanks.

My understanding so far is that:

  1. The programming model is event driven, especially the way it handles IO.
  2. It uses javascript and the parser is V8.
  3. It can be easily used to create concurrent server apps.

Are my understandings correct? If yes, then what are the benefits of evented IO, is it just more for the concurrency stuffs? Also is the direction of node.js to become a framework like, javascript based (v8 based) programming model?

A: 

Well, I understand that

  • Node's goal is to provide an easy way to build scalable network programs.
  • Node is similar in design to and influenced by systems like Ruby's Event Machine or Python's Twisted.
  • Evented I/O for V8 javascript.

For me that means that you were correct in all three assumptions. The library sure looks promising!

nes1983
A lot of the times I find about page is quite vague.
Jeffrey C
+12  A: 

v8 is an implementation of javascript, it lets you run standalone javascript apps (among other things).

node.js is simply a library written for v8 which does evented I/O. This concept is a bit trickier to explain and I'm sure someone will answer with a better explanation than I... The gist is that rather than doing some input or output and waiting for it to happen, you just don't wait for it to finish. So for example, ask for the last edited time of a file:

// pseudo code
stat( 'somefile' )

That might take a couple milliseconds, or it might take seconds. With evented IO you simply fire off the request and instead of waiting around you attach a callback that gets run when the request finishes:

// pseudo code
stat( 'somefile', function( result ) {
  // use the result here
} );
// ...more code here

This makes it a lot like javascript in the browser (eg, with Ajax style functionality).

For more information you should check out this article which was my introduction to the library/platform... I found it quite good.

thenduks
How is evented IO implemented without using locks, using threading, process, closures? And I have a feeling that the concepts are quite similar to that of functional programming and Erlang.
Jeffrey C
It's implemented as a simple event loop, as far as I know. v8 has the callback/etc functionality already, just like any javascript implementation.
thenduks
The IO event loop of node.js means that at any given point of time at most only one thing is being done. I see two significant gains: There is no overhead of thread switching, so node.js is very fast, and secondly many typical concurrency bugs Java is notorious for are not possible.
nalply
"How is evented IO implemented without using ... closures?"JavaScript supports closures and they are used all the time in node.js (anonymous functions ans in the example here).
panzi
@panzi: Didn't notice Jeffrey included closures in his list of things node.js is implemented 'without'. Obviously every function in javascript is a closure around it's scope :)
thenduks
+16  A: 

I think the advantages are:

  1. Web development in a dynamic language (JavaScript) on a VM that is incredibly fast (V8). It is much faster than Ruby, Python, or Perl.

  2. Ability to handle thousands of concurrent connections with minimal overhead on a single process.

  3. JavaScript is perfect for event loops with first class function objects and closures. People already know how to use it this way having used it in the browser to respond to user initiated events.

  4. A lot of people already know JavaScript, even people who do not claim to be programmers. It is arguably the most popular programming language.

  5. Using JavaScript on a web server as well as the browser reduces the impedance mismatch between the two programming environments which can communicate data structures via JSON that work the same on both sides of the equation. Duplicate form validation code can be shared between server and client, etc.

postfuturist
I was about to ask the same question. I am still not clear about this but I think I need to see a working application or better yet write one. Is it possible to do database lookups?
iHeartDucks
Regarding #1, do you have or know of any actual metrics showing that code running on V8 is faster than similar code in Python? My intuition tells me that you are correct, but some actual numbers would be a huge help in influencing the decision makers.
Adam Crossland
@Adam Crossland: You can compare V8 to some other major programming language implementations at http://shootout.alioth.debian.org/ . It beats Python 3 and Ruby 1.9 pretty handily for most benchmarks.
postfuturist
@postfuturist: It actually performs well against lots of alternatives. It beats Java 6 pretty handily in lots of cases, too. Neat!
Adam Crossland
+3  A: 

The closures are a way to execute code in the context it was created in.

what this means for concurency is that you can define variables, then initiate a nonblocking IO funciton, and send it an anonymous function for it's callback.

when the task is complete, the callback function will execute in the context with the variables, this is the closure.

The reason closures are so good for writing apps with nonblocking IO is that it's very easy to manage the context of functions executing asynchronously.

Fire Crow