There's a new kid in town: node.js

Preview:

Citation preview

There's a new kid in town

and his name is

NodeJS

Mike Amundsen @mamund

Setting the Stage

• Mike Amundsen

• Goals

• Non-Goals

• Bottom Line

Mike Amundsen

• Presenter, Architect, Programmer • Current Focus: "Hypermedia" • Book Project ...

• Designing Hypermedia APIs

with HTML5 and NodeJS - O’Reilly, late 2011

Goals

• Introduce NodeJS

• Talk about why NodeJS is "different" • See some NodeJS examples

• Encourage you to try it out yourself

Non-Goals

• Details of downloading and installing NodeJS

• Defending NodeJS programming model • Learning Javascript • Writing modules or contributing to NodeJS

Bottom Line

• NodeJS is not 'business as usual' • Javscript is not the interesting part of NodeJS

• NodeJS represents an emerging trend

• You should try it!

WAIT...

We spend a lot of our time waiting...

WAIT...

For someone else to do stuff...

WAIT...

So we can get our stuff done.

WAIT...

And that's a waste of valuable resources.

And that's a bummer!

I wonder, is there another way to do this?

Ryan Dahl sez "Yes!"

NodeJS

Why NodeJS is Different

Threads

• Most Web frameworks use "threads" to handle work

• One thread for each request • Multiple threads for concurrent execution

• There is a limit to the number of threads

• But as long as requests are short, it's not a problem

It used to be pretty simple...

• Local files

• Local database

• Single data source

But then along came the cloud...

• Remote files (Amazon S3) • Remote data (SQL Azure) • Multiple data sources (Twitter, Flickr, Disqus, etc.)

All that stuff means more waiting...

And that means longer requests...

And that's a bummer!

So NodeJS does it another way

• Scavenge the "wait states" as execution time

• Use a single loop (queue) 1. Execute a request 2. Go to the next item in the queue

3. Go to step 1

• No more waiting!

It's called the Reactor Pattern

This works great for the cloud because...

• Requests usually involve waiting

• Requests are usually "side-effect free" (state-less) • Most of the "work" is done by someone else (remote

servers)

There, I fixed it!

Wait a minute<g>; how do you code this?

Getting started is easy...

• Starting a request is no problem

• But you need to be ready when the remote server is done

• You need to register a location for the returning call

You need a "callback"

If only there was a programming language that everyone already knew that also handled callbacks easily...

Let's see...

• Scheme

• Erlang

• Scala

• C++

• ... • There's at least one more I know of...

There, I fixed it again!

Javascript is a logical choice for Node

• It's already well-known (if not well-understood<g>) • There are multiple JS "engines" (Node uses V8) • Javascript already supports callbacks and closures

So, let's review...

So, let's review...

NodeJS uses the Reactor Pattern (event loop) to take advantage of the extended wait-states that are now more common with cloud-based implementations.

So, let's review...

NodeJS uses the Reactor Pattern (event loop) to take advantage of the extended wait-states that are now more common with cloud-based implementations. And NodeJS uses Javascript on the "front-end" because it supports callbacks/closures, is readily available, and well-known.

Ok, let's see some code!

NodeJS code is pretty straightforward

NodeJS code is pretty straightforward

Creating an HTTP file server is easy.

Writing a file uploader is even easier.

You can write clients w/ Node, too.

NodeJS has a package system: npm

Node Package Manager

And lots of modules...

For building full-featured applications

Check out these resources...

Home Page & Discussion Group

• NodeJS Site (http://nodejs.org) • Discussion Group

(https://groups.google.com/forum/#!forum/nodejs)

Books & Tutorials

• Hands On NodeJS (Pedro Teixeira) • Up and Running w/ Node

(http://ofps.oreilly.com/titles/9781449398583/) • NodeTuts (http://nodetuts.com/)

Online Hosting

• JSApp.US (http://jsapp.us/) - *instant*

• Joyent (https://api.no.de/) • Nodester (http://nodester.com/) • DotCloud (http://www.dotcloud.com/) • NodeSocket (http://www.nodesocket.com/) *nya*

• NodeJitsu (http://www.nodejitsu.com/#/home) *nya*

In Summary...

NodeJS

• Based on the "Reactor Pattern" • Uses Javascript on the front-end

• Is well-suited for cloud-based implementations

• Completely Open-Source (free) • Built on Linux • Windows Native port soon

• Represents a growing trend

There's a new kid in town

and his name is

NodeJS

Mike Amundsen @mamund

Recommended