Async I/O & Node.js
Felix & pfleidi
We like:
- JavaScript
- Memes
- High Level Programming
- Buzzword-Bingo!
Be prepared!
Webserver
Single Threaded Process
sock := listen(8080)
while True:
request := sock.accept()
file := parse(request)
data := open(file).read()
request.send(data)
Does it scale?
No Dave, I'm afraid I can't do that!
Worker Processes
Apache mpm-prefork
Benchmark
Meh ...
Worker Threads
Apache mpm-worker
Benchmark
Better, but still ...
Async I/O
NginX
Benchmark
Squeee ...
- Google V8 engine
- libev
- CommonJS Module System
- Async libraries
JavaScript SRSLY?
Skeptical?
JavaScript Goodies
- Fast Highlevel Language
- Prototype-based OOP
- Functions are first-class citizens
- Conceptual purity
Still not convinced?
John Resig is a Ninja!
Douglas Crockford is Chuck Norris!
Yo dawg! I herd you like events ...
... so we put an event loop in ur JS
Combined Awesomeness!
Usage of Node.js
- Scripting Network Services
- Web Applications
- Crawlers
- Automated Media-Processing
Maybe even serious applications
What does it look like?
Sync
var data = fs.readFileSync(
filePath, "binary"
); //blocking
doSomethingGreat(data);
Async + Callback
fs.readFile(filePath, "binary",
function(err,data){ //non-blocking
if (err) throw err;
doSomethingGreat(data);
}
);
doMoreAwesomeness();
HTTP "Hello World"
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Does it work?
curl http://localhost:8124/
Hello World
curl -I http://localhost:8124/
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Node Modules
- WebSockets/Comet/AJAX
- Web Frameworks
- XMPP/IRC/Chat
More Modules
- Database Stuff
- Template Processing
- Parser Generators
- Much much more ...
Our Projects:
- (Fucking)Magnets
- Mediengewitter
Magnets
What does it do?
- Modular crawler
- Really efficient image downloader
Mediengewitter
What does it do?
- Websocket server
- Image push framework
Questions?