Search

1/04/2010

Comet with node.js and V8 - amix blog

Comet with node.js and V8 - amix blog, Youtube - Amir Salihefendic: Comet with node.js 1/6

What makes V8 special
* V8 JavaScript VM is sued in Google Chrome and is developed by a small Google team in Denmark. V8 is open-source.
* V8 team is led by Lars Bak, one of the leading VM engineers in the world with 20 years of experience in building VMs.
* Lars Bak was the technical lead behind HotSpot (Sun's Java VM). HotSpot improved Java's peroformance 20x times.
* Before HotSpot Lars Bak worked on a Samlltalk VM.
* No JIT, all JavaScript is compiled to assembler.
* Hidden classes optimization from Self (V8 does not dynamically lookup access properties, instead it uses hidden classes that are created behind the scene)
* Improved garbage collector. (stop-the-world, generational, accurate, garbage collector)
* Remarks/ "limitations": No bytecode language, no threads, no processes.

What is node.js
* A sysmtem built on top of v8
* The non-blocking nature makes node.js a good fit for comet and next generation realtime web-applications
* 8000 lines of C/C++, 2000 lines of JavaScript, 14 contributors.

Advantages of non-blocking
* nginx: non-blocking, apache: threaded.
* non-bocking can handle more req.pr.sec and uses a lot less memory.
* comet does not scale at all for threaded servers ...

Major point
* JavaScript programming is already geared towards event based programming:
* Closures (anonymouse functions) are natural part of the language.

Blocking vs. non-blocking
* The way to do it in most other languages:

puts("Enter your name: ")
var name = gets();
puts("Name: " + name)

* The way to do it in node.js!

puts("Enter your name: ")
gets(function(name) {
puts("Name: " + name)
})

* nodes.js design philosophy: To receive data from disk, network or another process there must be a callback.

Even in node.js
* All objects which emit events are instance of process.EventEmitter
* A promise is a EventEmitter which emits either success or error, but not both

var tcp = require("tcp");
var s = tcp.createServer();
s.addListener("connection", function (c) {
c.send("hello nasty!");
c.close()
});
s.listen(8000);


var stat = require("posix").stat,
puts = require("sys").puts;

var promise = stat("/etc/passwd");
promise.addClassback(function (s) {
puts("modified: " + s.mtime);
});
promise.addErrback(function(orgi_promise) {
puts("Could not fetch stats on /etc/passwd")
});


Implmenting comet
* Long polling
- works for most parts. Used in Plurk
- is more expensive and problematic than streaming
* Streaming without WebSockets:
- very problematic due to proxies and firewalls.
* Streaming with WebSockets (HTML5):
- Makes comet trivial on both client and server side

WebSockets
* Aims to expose TCP/IP sockets to browsers, while respecting the constraints of the web (security, proxies and firewalls)
* A thin layer on top of TCP/IP that adds:
- orgin based security model
* Addressing and protocol naming, mechanism for supporting multiple services on one port and multiple host names on one IP address
* framing mechanism for data transportation.

Other comet servers
* JBoss Netty: Java library for doing non-blocking networking, based on java.nio
* erlycomet: Erlang comet server based on MochiWeb
* Tornado: FriendFeed's Python non-blocking server.


via: Amir Salihefendic 演講: Comet with node.js 影片 與 筆記

沒有留言: