Save 37% off PRO during our Black Friday Sale! »

Async NodeJS

Async NodeJS

34be88398f623c109b61d23e8215bd23?s=128

Mariusz Gil

March 16, 2013
Tweet

Transcript

  1. Async sfi 2013 krakow Mariusz gil

  2. about me

  3. about the problem

  4. ? user browser application click request response ajax

  5. database server database server 200 m 200 m 400 m

    browser application request response ajax
  6. 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
  7. about the solution ...one of them

  8. event-driven non-blocking I/O server-side javaScript environment based on V8

  9. event driven

  10. event driven

  11. event driven

  12. ? ? ? ? ? ? ? ? ?? ?

    ? what the...? ?
  13. event machine twisted

  14. single thread events queue event loop callbacks

  15. request routing output formatting database query api call caching logging

    data processing
  16. https://npmjs.org/ node packaged modules 25 178 packages underscore async request

    coffee-script express socket.io 1.4m downloads per day
  17. 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
  18. 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/');
  19. 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');
  20. 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); });
  21. 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); }
  22. 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'); });
  23. 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');
  24. lightweight web framework like sinatra or silex express

  25. realtime communication library socket.io

  26. best use cases single page a lication lightweight API ma

    co ection serve non-blocking I/O a long p l
  27. from rails to node mobile backends

  28. problems... callbacks hell libraries deployment garbage collector current version is

    0.10.0
  29. 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)) } }) }) } })
  30. var async = require('async'); async.parallel([ function(callback) { ... }, function(callback)

    { ... } ], callback); var async = require('async'); async.waterfall([ function(callback) { ... }, function(arg, callback) { ... } ], callback);
  31. thanks! feel free to contact with me email: mariusz@mariuszgil.pl twitter:

    @mariuszgil