why node.js

69

Click here to load reader

Upload: guileen

Post on 10-May-2015

4.461 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Why Node.js

Why

by 桂林

桂糊涂@weibo

guilin1981@twitter

Page 2: Why Node.js

About meAbout me

2003, Delphi.

2006, Java.

2009, Python.

2010, Node.js.

Author of mongoskinmongoskin and stormjsstormjs

Page 3: Why Node.js

What is node.js?What is node.js?

Evented I/Ofor

V8 JavaScript.

Page 4: Why Node.js

What can node.js do?What can node.js do?

Page 5: Why Node.js

What can node.js do?What can node.js do?

Node.js as webserver

Page 6: Why Node.js

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Page 7: Why Node.js

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Page 8: Why Node.js

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Page 9: Why Node.js

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Node.js as TCP server

Page 10: Why Node.js

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Node.js as TCP server

Node.js as CLI tools

Page 11: Why Node.js

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Node.js as TCP server

Node.js as CLI tools

Node.js as GUI tools

Page 12: Why Node.js

Why node.jsWhy node.js

Why asynchronous IO?Why asynchronous IO?

Page 13: Why Node.js

Why asynchronous IO?Why asynchronous IO?IO is the bottle-neck of application.

Page 14: Why Node.js

Why asynchronous IO?Why asynchronous IO?IO is the bottle-neck of application.

Network IO

File IO

Database IO

Page 15: Why Node.js

Blocking socket IOBlocking socket IOclass ServerThread extends Thread { public ServerThread(socket){ this.socket = socket; } public void run(){ BufferedReader reader = new BufferedReader(this.socket.getInputStream()); String line; while((line = reader.readLine()) != null){ // block here // handle line } }}...ServerSocket serverSocket = new ServerSocket(port);while (true) { Socket s = serverSocket.accept(); // block here new ServerThread(s).start();}

Page 16: Why Node.js

Non-blocking socket IONon-blocking socket IOnet.createServer(function(socket){ socket.on('data', function(data){ // handle data }); socket.on('close', function(has_error){ // handle close }); socket.on('error', function(err){ // handle error });}).listen(port);

Page 17: Why Node.js

Non-blocking IO vs Blocking IONon-blocking IO vs Blocking IO

nginx(non-blocking) vs apache(blocking)

Page 18: Why Node.js

Node.js IO APINode.js IO API

stream

fs

http

net

dns

Page 19: Why Node.js

Why node.jsWhy node.js

Why asynchronous IO?

Why javascr ipt?Why javascr ipt?

Page 20: Why Node.js

Why javascript?Why javascript?

Page 21: Why Node.js

Why javascript?Why javascript?

Page 22: Why Node.js

Why javascript?Why javascript?

Page 23: Why Node.js

Why javascript?Why javascript?

Browsers war improving javascript.

Page 24: Why Node.js

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Page 25: Why Node.js

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Javascript is one of the most popularmost popular programming language.

Page 26: Why Node.js

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Javascript is one of the most popularmost popular programming language.

Javascript has c losureclosure(anonymoused function).

Page 27: Why Node.js

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Javascript is one of the most popularmost popular programming language.

Javascript has c losureclosure(anonymoused function).

Javascript is cross-platform.

Page 28: Why Node.js

Why node.jsWhy node.js

Why asynchronous IO?

Why javascript?

Why v8?Why v8?

Page 29: Why Node.js

About v8About v8

Page 30: Why Node.js

About v8About v8

V8 javascript VM is used in Google ChromeGoogle Chrome.

Page 31: Why Node.js

About v8About v8

V8 javascript VM is used in Google ChromeGoogle Chrome.

V8 team is led by Lars BakLars Bak , one of the leading VM engineers in theworld with 20 years of experience in building VM.

Page 32: Why Node.js

About v8About v8

V8 javascript VM is used in Google ChromeGoogle Chrome.

V8 team is led by Lars BakLars Bak , one of the leading VM engineers in theworld with 20 years of experience in building VM.

Lars BakLars Bak was the technical leadtechnical lead behind HotSpot(Sun’s Java VM).HotSpot improved Java’s performance 20x t imes20x t imes.

Before HotSpot, Lars BakLars Bak worked on a smalltalk VMsmalltalk VM .

Page 33: Why Node.js

How fast v8 is?How fast v8 is?

Fast Property Access

Dynamic Machine Code Generation

Efficient Garbage Collection

Page 34: Why Node.js

V8 exampleV8 exampleThe JavaScript code to access property x from a Point object is:point.x

Page 35: Why Node.js

V8 exampleV8 exampleThe JavaScript code to access property x from a Point object is:point.x

In V8, the machine code generated for accessing x is:# ebx = the point objectcmp [ebx,<hidden class offset>],<cached hidden class>jne <inline cache miss>mov eax,[ebx, <cached x offset>]

Page 36: Why Node.js

Why node.jsWhy node.js

Why asynchronous IO?

Why javascript?

Why v8?

Why threadless?Why threadless?

Page 37: Why Node.js

Why threadless?Why threadless?

Page 38: Why Node.js

Why threadless?Why threadless?

2mb stack per thread

Page 39: Why Node.js

Why threadless?Why threadless?

2mb stack per thread

Dead-locking

Page 40: Why Node.js

Why threadless?Why threadless?

2mb stack per thread

Dead-locking

Thread-safe?

Page 41: Why Node.js

Why threadless?Why threadless?

2mb stack per thread

Dead-locking

Thread-safe?

Thread is design for blocking IO.

Page 42: Why Node.js

Why node.jsWhy node.js

Why asynchronous IO?

Why javascript?

Why v8?

Why threadless?

Why node. js special?Why node. js special?

Page 43: Why Node.js

Why node.js special?Why node.js special?Pythoner:

Page 44: Why Node.js

Why node.js special?Why node.js special?Pythoner:

Python is one of the most popular dynamic language too.

Page 45: Why Node.js

Why node.js special?Why node.js special?Pythoner:

Python is one of the most popular dynamic language too.

Performance of python is OK.

Page 46: Why Node.js

Why node.js special?Why node.js special?Pythoner:

Python is one of the most popular dynamic language too.

Performance of python is OK.

We have good non-blocking web framework.

Page 47: Why Node.js

Tornado by facebookTornado by facebook

Tornado is a non-blocking web server, open-source by facebook.

Page 48: Why Node.js

Hello TornadoHello Tornadoimport tornado.ioloopimport tornado.web

class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world")

application = tornado.web.Application([ (r"/", MainHandler),])

if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()

Page 49: Why Node.js

Hello NodejsHello Nodejsvar http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Node.js\n');}).listen(3000, "127.0.0.1");

Page 50: Why Node.js

Tornado with IOTornado with IOimport pymongoimport tornado.web

class Handler(tornado.web.RequestHandler): @property def db(self): if not hasattr(self, '_db'): # block here self._db = pymongo.Connection( host='127.0.0.1', port=27017).test return self._db

def get(self): try: # block here user = self.db.users.find_one({'username': self.current_user}) self.render('template', full_name=user['full_name']) except: raise tornado.web.HTTPError(500)

Page 51: Why Node.js

Tornado with async IOTornado with async IOimport asyncmongoimport tornado.web

class Handler(tornado.web.RequestHandler): @property def db(self): if not hasattr(self, '_db'): self._db = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', port=27017, maxcached=10, maxconnections=50, dbname='test') return self._db

@tornado.web.asynchronous def get(self): self.db.users.find({'username': self.current_user}, limit=1, callback=self._on_response)

def _on_response(self, response, error): if error: raise tornado.web.HTTPError(500) self.render('template', full_name=respose['full_name'])

Page 52: Why Node.js

Node.js with IONode.js with IOAnonymoused function, is very important for Evented IOvar mongo = require('mongoskin'), http = require('http'), jade = require('jade'), db = mongo.db('localhost/test');

http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); db.users.findOne({'username': req.session.current_user}, function(err, user){ jade.renderFile('template', {user: user}, function(err, html){ res.end(html); }); });}).listen(3000, "127.0.0.1");

Page 53: Why Node.js

What make node.js special?What make node.js special?

Page 54: Why Node.js

What make node.js special?What make node.js special?

Javascript is special.

Page 55: Why Node.js

What make node.js special?What make node.js special?

Javascript is special.

Most of node.js modules are asynchronoused.

Page 56: Why Node.js

What make node.js special?What make node.js special?

Javascript is special.

Most of node.js modules are asynchronoused.

It’s easy to write threadless module.

Page 57: Why Node.js

What make node.js special?What make node.js special?

Javascript is special.

Most of node.js modules are asynchronoused.

It’s easy to write threadless module.

Node.js community is very active.

Page 58: Why Node.js

How many modules?How many modules?

2320 modules can beinstall via npm2011, June 07.

Page 59: Why Node.js

Node.js communityNode.js community

What do you thinkabout nodecommunity?

Page 60: Why Node.js

Arunoda:Arunoda:

Its the freedom followed by tools like github and npm. And everythingis growing fast.

Page 61: Why Node.js

Pau:Pau:

People is more open minded than in other programming communities.There is people coming from ruby, python, c, haskell, java, erlang,php…

Page 62: Why Node.js

Mikeal Rogers:Mikeal Rogers:

People using node.js are building stuff. the project seeks to make thelives better of people building products.

Page 63: Why Node.js

Mikeal Rogers:Mikeal Rogers:

People using node.js are building stuff. the project seeks to make thelives better of people building products.My experience with Python and Ruby is that their primary reason forworking on the project is around with a new language and/or vm. thepeople who work on “core” don’t build products and probably neverwill.

Page 64: Why Node.js

Mikeal Rogers:Mikeal Rogers:

People using node.js are building stuff. the project seeks to make thelives better of people building products.My experience with Python and Ruby is that their primary reason forworking on the project is around with a new language and/or vm. thepeople who work on “core” don’t build products and probably neverwill.Node.js is not a language and it doesn’t write it’s own vm. it’s notattractive to people who don’t care about building stuff with it.

Page 65: Why Node.js

桂林:桂林:

潮不等于装13

Page 66: Why Node.js

桂林:桂林:

潮不等于装13 , node.js很酷,也很务实。

Page 67: Why Node.js

Why node.jsWhy node.js

Page 68: Why Node.js

Why node.jsWhy node.js

Why asynchronous IO?Never blocking, cpu efficient

Why javascript?Right and popluar language

Why v8?Extremely fast VM

Why threadless?Easy, memory efficient

Why node.js special?Active and creative community

Page 69: Why Node.js

Thank youThank you