2. What is ?
“Evented I/O for V8 Javascript” – http://nodejs.org
Application engine for “scalable network applications”
Really freaking fast:
One process, zero* threads
(Almost) no blocking
* actually, there is a thread pool. We’ll get to that.
3. What is ?
High concurrency with low overhead
No thread swapping or context switching
Applications must be designed from the ground up to
not block on I/O, CPU-intensive work, etc.
4. What Node.js Isn’t
A full-stack web framework (a la Ruby on Rails)
But! you can get there:
ExpressJS: (http://expressjs.com/) (Web framework
with MongoDB support built in - similar to Sinatra)
http://github.com/senchalabs/connect (Rack-like
middleware for Node and Express)
haml.js: http://github.com/visionmedia/haml.js (Pure
js implementation of HAML)
5. What Node.js Isn’t (cont.)
Great at sending large static files concurrently. (use
nginx.)
Buffer implementation in recent versions improves
this dramatically: http://nodejs.org/jsconf2010.pdf
Completely non-blocking.
CPU-intensive stuff will still block you (do it in a
worker process).
Some system calls (executed in a thread pool)
7. &
Great together! Both:
speak Javascript
understand JSON documents
are freaking fast.
So, what should you build?
8. &
You can build: Standard n-tier web applications
Great way to learn mongoDB and Node
Go build a blog engine! (Everyone does.)
Lots of libraries to help you get started: ExpressJS,
Connect, etc.
9. &
You should build:
Applications requiring high throughput and
concurrency
Applications with real-time needs
Applications with long-polling connections
Real-time multiuser chat
Web or log analytics (e.g. Hummingbird)
10. Getting Started
Grab the following:
mongoDB: http://www.mongodb.org/downloads
Node.js: http://nodejs.org/#download
npm, a package manager for Node:
curl http://npmjs.org/install.sh | sh
11. Getting Started
Use npm to install a mongoDB driver for Node:
npm install mongodb
Tons of other fun stuff as well - full package list at
http://npm.mape.me/
20. Other Fun Stuff:
http://wiki.github.com/ry/node/modules (Need
extensions? We got 'em.)
Routers, file servers, frameworks, crypto...
http://socket.io/: Cross-platform WebSockets
Supports HTML5 websockets
Falls back to Flash / AJAX in less capable browsers
How many people have used Node.js? In production?
Start with V8: http://code.google.com/p/v8/
Chrome’s javascript engine (Fast, aggressively developed)
Add Evented I/O, system calls, HTTP libraries
First created by Ryan Dahl @ Joyent
“Scalable network applications”: tens of thousands of simultaneous connections on a single box
High concurrency apps are likely to encounter the C10k problem:
http://www.kegel.com/c10k.html
Overhead per connection: An order of magnitude smaller than most libraries’ thread stack allocations.
Builds on the approaches used by libasync, twisted, evenmachine, etc.
Use kernel level event notification (kqueue, epoll, etc.) to notify when files, network connections etc. are ready to be used. App can do other work rather than blocking.
Node shares more with Twisted than Rails
nginx is amazing at handling static content.
Buffers are the suggested way for handling binary data, large data, etc.
The Buffer API is detailed here: http://nodejs.org/api.html#buffers-2
Mongo’s shell is javascript, can execute javascript in process
You can actually write your entire application in javascript - front end and back!
In fact, there are several blog engines already. Try Wheat.
Of course, building your UI on top of Node.js isn’t a bad idea if the rest of your app fits as well - no need to run a Rails app next to your Node app just for a GUI. (cf. Hummingbird).
The current js driver supports master and slave connections, atomic operations, map/reduce...
Chained execution of callbacks is how most every Node program will work.
Recent builds of Node support udp sockets.
Mongo’s great at storing semi-structured data quickly
As much fun as quadruply-nested callbacks are.
As much fun as quadruply-nested callbacks are.
Hydrating documents loaded from the db converts them into instances of our models.
Looks almost like Rails...