Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Async NodeJS

Async NodeJS

Mariusz Gil

March 16, 2013
Tweet

More Decks by Mariusz Gil

Other Decks in Technology

Transcript

  1. database server database server 200 m 200 m 400 m

    browser application request response ajax
  2. i/o ops are expensive l1-cache 3 cycles l2-cache 14 cycles

    ram memory 250 cycles disk 41 000 000 cycles network 240 000 000 cycles Source: Ryan Dahl’s 2008.11.08 node.js presentation
  3. ? ? ? ? ? ? ? ? ?? ?

    ? what the...? ?
  4. https://npmjs.org/ node packaged modules 25 178 packages underscore async request

    coffee-script express socket.io 1.4m downloads per day
  5. apt-get install python g++ make mkdir ~/nodejs && cd $_

    wget -N http://nodejs.org/dist/node-latest.tar.gz tar xzvf node-latest.tar.gz && cd `ls -rd node-v*` ./configure make install sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs npm
  6. var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type':

    'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');
  7. var net = require('net'); var server = net.createServer(function (socket) {

    socket.write('Echo server\r\n'); socket.pipe(socket); }); server.listen(1337, '127.0.0.1');
  8. var fs = require('fs'); var data = fs.readFileSync('sample.txt'); console.log(data); var

    fs = require('fs'); fs.readFile('sample.txt', function(err, data) { console.log(data); });
  9. var cluster = require('cluster'); var http = require('http'); var numCPUs

    = require('os').cpus().length(); if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function (worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { // Workers can share any TCP connection // In this case its a HTTP server http.createServer(function(req, res) { res.writeHead(200,{'Content-Type': 'text/plain'}); res.end(”hello world\n”); }).listen(8000); }
  10. var mysql = require('mysql'); var connection = mysql.createConnection({ host :

    'localhost', user : 'root', password : '', socketPath : '/opt/local/var/run/mysql55/mysqld.sock' }); connection.connect(); var query = connection.query('SELECT * FROM sakila.actor'); query .on('result', function(row) { connection.pause(); function processRow(row, callback) { console.log(row); callback(); } processRow(row, function() { connection.resume(); }); }) .on('end', function() { console.log('end\n'); });
  11. var events = require('events'); var emitter = new events.EventEmitter(); emitter.on('textEvent',

    function(data) { console.log(data); }); emitter.on('dataEvent', function(data) { console.log(data); }); emitter.emit('textEvent', 'Start'); emitter.emit('dataEvent', new Array(1, 2, 3, 4, 5)); emitter.emit('textEvent', 'End');
  12. best use cases single page a lication lightweight API ma

    co ection serve non-blocking I/O a long p l
  13. callback hell http://callbackhell.com/ fs.readdir(source, function(err, files) { if (err) {

    console.log('Error finding files: ' + err) } else { files.forEach(function(filename, fileIndex) { console.log(filename) gm(source + filename).size(function(err, values) { if (err) { console.log('Error identifying file size: ' + err) } else { console.log(filename + ' : ' + values) aspect = (values.width / values.height) widths.forEach(function(width, widthIndex) { height = Math.round(width / aspect) console.log('resizing ' + filename + 'to ' + height + 'x' + height) this.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err) { if (err) console.log('Error writing file: ' + err) }) }.bind(this)) } }) }) } })
  14. var async = require('async'); async.parallel([ function(callback) { ... }, function(callback)

    { ... } ], callback); var async = require('async'); async.waterfall([ function(callback) { ... }, function(arg, callback) { ... } ], callback);