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

React (phpnw)

React (phpnw)

A4b95be2145cc46f891707b6db9dd82d?s=128

Igor Wiedler

October 06, 2012
Tweet

Transcript

  1. Saturday, October 6, 12

  2. @igorwesome Saturday, October 6, 12

  3. Saturday, October 6, 12

  4. Saturday, October 6, 12

  5. Saturday, October 6, 12

  6. var http = require('http'); var server = new http.Server(); server.on('request',

    function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(1337, '127.0.0.1'); Saturday, October 6, 12
  7. Saturday, October 6, 12

  8. Saturday, October 6, 12

  9. $server = stream_socket_server('tcp://127.0.0.1:1337'); while ($conn = stream_socket_accept($server, -1)) { fwrite($conn,

    "HTTP/1.1 200 OK\r\n"); fwrite($conn, "Content-Length: 3\r\n\r\n"); fwrite($conn, "Hi\n"); fclose($conn); } Saturday, October 6, 12
  10. $s=stream_socket_server('tcp://[::1]:81'); while($c=stream_socket_accept($s,-1)) fwrite($c,"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nHi"); Saturday, October 6, 12

  11. Non-blocking I/O Saturday, October 6, 12

  12. C10K Saturday, October 6, 12

  13. Calculations are fast, I/O is slow. Saturday, October 6, 12

  14. Latency Comparison Numbers -------------------------- L1 cache reference 0.5 ns Branch

    mispredict 5 ns L2 cache reference 7 ns Mutex lock/unlock 25 ns Main memory reference 100 ns Compress 1K bytes with Zippy 3,000 ns Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms Read 4K randomly from SSD* 150,000 ns 0.15 ms Read 1 MB sequentially from memory 250,000 ns 0.25 ms Round trip within same datacenter 500,000 ns 0.5 ms Read 1 MB sequentially from SSD* 1,000,000 ns 1 ms Disk seek 10,000,000 ns 10 ms Read 1 MB sequentially from disk 20,000,000 ns 20 ms Send packet CA->Netherlands->CA 150,000,000 ns 150 ms Saturday, October 6, 12
  15. Latency Comparison Numbers -------------------------- L1 cache reference 0.5 ns Branch

    mispredict 5 ns L2 cache reference 7 ns Mutex lock/unlock 25 ns Main memory reference 100 ns Compress 1K bytes with Zippy 3,000 ns Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms Read 4K randomly from SSD* 150,000 ns 0.15 ms Read 1 MB sequentially from memory 250,000 ns 0.25 ms Round trip within same datacenter 500,000 ns 0.5 ms Read 1 MB sequentially from SSD* 1,000,000 ns 1 ms Disk seek 10,000,000 ns 10 ms Read 1 MB sequentially from disk 20,000,000 ns 20 ms Send packet CA->Netherlands->CA 150,000,000 ns 150 ms Saturday, October 6, 12
  16. Saturday, October 6, 12

  17. stream_select Saturday, October 6, 12

  18. $readable = $read ?: null; $writable = $write ?: null;

    $except = null; if (stream_select($readable, $writable, $except, 1)) { if ($readable) { foreach ($readable as $stream) { ... } } if ($writable) { foreach ($writable as $stream) { ... } } } Saturday, October 6, 12
  19. $readable = $read ?: null; $writable = $write ?: null;

    $except = null; if (stream_select($readable, $writable, $except, 1)) { if ($readable) { foreach ($readable as $stream) { ... } } if ($writable) { foreach ($writable as $stream) { ... } } } Event Loop Saturday, October 6, 12
  20. Saturday, October 6, 12

  21. $server = stream_socket_server('tcp://127.0.0.1:1337'); $read = [$server]; $write = []; ...

    // deep in the event loop foreach ($readable as $stream) { if ($server === $stream) { $conn = stream_socket_accept($server, 0); $read[] = $conn; } else { $data = fread($stream, 1024); echo $data; } } Saturday, October 6, 12
  22. Saturday, October 6, 12

  23. Saturday, October 6, 12

  24. Event-driven, non-blocking I/O with PHP. Saturday, October 6, 12

  25. $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new

    React\Http\Server($socket, $loop); $http->on('request', function ($req, $rep) { $rep->writeHead(); $rep->end("Hello World!\n"); }); $socket->listen(8080); $loop->run(); Saturday, October 6, 12
  26. $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $socket->on('connection', function ($conn)

    { $conn->pipe($conn); }); $socket->listen(4000); $loop->run(); Saturday, October 6, 12
  27. Saturday, October 6, 12

  28. # composer.json { "require": { "react/http": "0.2.*" } } Saturday,

    October 6, 12
  29. $ composer install Saturday, October 6, 12

  30. Saturday, October 6, 12

  31. Philosophy Saturday, October 6, 12

  32. Architecture Saturday, October 6, 12

  33. Événement EventEmitter for PHP Saturday, October 6, 12

  34. $emitter = new Evenement\EventEmitter(); $emitter->on('data', function ($data) { echo $data;

    }); $emitter->emit('data', [$data]); Saturday, October 6, 12
  35. EventLoop Stream Socket Http Saturday, October 6, 12

  36. EventLoop $loop = React\EventLoop\Factory::create(); $server = stream_socket_server('tcp://127.0.0.1:8080'); stream_set_blocking($server, 0); $loop->addReadStream($server,

    function ($server) use ($loop) { $conn = stream_socket_accept($server); ... }); Saturday, October 6, 12
  37. EventLoop $loop->addPeriodicTimer(5, function () { $memory = memory_get_usage() / 1024;

    $formatted = number_format($memory, 3).'K'; echo "Current memory usage: {$formatted}\n"; }); Saturday, October 6, 12
  38. EventLoop $loop->run(); Saturday, October 6, 12

  39. EventLoop backends stream_select libevent upcoming libev libuv Saturday, October 6,

    12
  40. ReadableStream isReadable() pause() resume() close() Events data, end, error, close

    Stream Saturday, October 6, 12
  41. ReadableStream isReadable() pause() resume() close() Events data, end, error, close

    Stream Saturday, October 6, 12
  42. WritableStream isWritable() write($data) end($data) close() Events drain, error, close, pipe

    Stream Saturday, October 6, 12
  43. WritableStream isWritable() write($data) end($data) close() Events drain, error, close, pipe

    Stream Saturday, October 6, 12
  44. Stream $source = new React\Stream\Stream(fopen('omg.txt', 'r'), $loop); $dest = new

    React\Stream\Stream(fopen('wtf.txt', 'w'), $loop); $source->pipe($dest); Saturday, October 6, 12
  45. Stream $a->pipe($b)->pipe($c); a | b | c Saturday, October 6,

    12
  46. Socket $socket = new React\Socket\Server($loop); $socket->on('connection', function ($conn) { $conn->write("Hello

    there!\n"); $conn->write("Welcome to this amazing server!\n"); $conn->write("Here's a tip: don't say anything.\n"); $conn->on('data', function ($data) use ($conn) { $conn->close(); }); }); $socket->listen(1337); Saturday, October 6, 12
  47. Http $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket); $http->on('request',

    function ($request, $response) { $response->writeHead(200, ['Content-Type' => 'text/plain']); $response->end("Hello World!\n"); }); $socket->listen(1337); Saturday, October 6, 12
  48. Http $http = new React\Http\Server($socket); $http->on('request', function ($request, $response) {

    $response->writeHead(200, ['Content-Type' => 'text/plain']); $request->pipe($response); }); Saturday, October 6, 12
  49. $waiting = null; $socket->on('connection', function ($conn) use (&$waiting) { if

    (null === $waiting || !$waiting->isReadable()) { $waiting = $conn; $conn->write("Please wait until a partner connects.\n"); return; } $message = "You are now talking to %s.\n"; $conn->write(sprintf($message, getConnectionId($waiting))); $waiting->write(sprintf($message, getConnectionId($conn))); $conn->pipe($waiting)->pipe($conn); $waiting = null; }); Saturday, October 6, 12
  50. Saturday, October 6, 12

  51. Saturday, October 6, 12

  52. React in the industry: It’s like, so amazing, man. Saturday,

    October 6, 12
  53. Saturday, October 6, 12

  54. Saturday, October 6, 12

  55. Saturday, October 6, 12

  56. Saturday, October 6, 12

  57. Saturday, October 6, 12

  58. Saturday, October 6, 12

  59. React in the industry: It’s like, so amazing, man. Saturday,

    October 6, 12
  60. React in the industry: Saturday, October 6, 12

  61. React/ZMQ Saturday, October 6, 12

  62. $context = new React\ZMQ\Context($loop); $push = $context->getSocket(ZMQ::SOCKET_PUSH); $push->connect('tcp://127.0.0.1:5555'); $push->send('hello'); Saturday,

    October 6, 12
  63. $context = new React\ZMQ\Context($loop); $pull = $context->getSocket(ZMQ::SOCKET_PULL); $pull->bind('tcp://127.0.0.1:5555'); $pull->on('message', function

    ($msg) { echo "Received: $msg\n"; }); Saturday, October 6, 12
  64. React/STOMP Saturday, October 6, 12

  65. React/Whois Saturday, October 6, 12

  66. Wisdom Saturday, October 6, 12

  67. Saturday, October 6, 12

  68. $foo->get(function ($bar) { $bar->get(function ($baz) { $baz->get(function ($bazinga) { $bazinga->get(function

    () { throw new FuckItException(); }); }); }); }); Saturday, October 6, 12
  69. React/Async Saturday, October 6, 12

  70. use React\Async\Util as Async; Async::waterfall([ function ($callback) use ($foo) {

    $foo->get($callback); }, function ($bar, $callback) { $bar->get($callback); }, function ($baz, $callback) { $baz->get($callback); }, function ($bazinga, $callback) { $bazinga->get($callback); }, function ($callback) { throw new FuckItException(); } ]); Saturday, October 6, 12
  71. React/Curry Saturday, October 6, 12

  72. use React\Curry\Util as Curry; $add = function ($a, $b) {

    return $a + $b; }; $addOne = Curry::bind($add, 1); echo sprintf("%s\n", $addOne(5)); // outputs 6 Saturday, October 6, 12
  73. public function query($domain, $callback) { Async::waterfall( [ Curry::bind([$this, 'resolveWhoisServer'], $domain),

    Curry::bind([$this, 'queryWhoisServer'], $domain), ], $callback ); } public function resolveWhoisServer($domain, $callback) public function queryWhoisServer($domain, $ip, $callback) Saturday, October 6, 12
  74. React in the industry: Saturday, October 6, 12

  75. Ratchet Saturday, October 6, 12

  76. Predis/Async Saturday, October 6, 12

  77. $client->connect(function ($client) { $client->monitor(function ($event) { echo implode(' ', [

    "[{$event->timestamp}]", $event->command, $event->arguments, ]); echo "\n"; }); }); Saturday, October 6, 12
  78. DNode-PHP Saturday, October 6, 12

  79. Huston we have a blocking call Saturday, October 6, 12

  80. DNS Saturday, October 6, 12

  81. Http Server Worker Client Worker Worker Client Client HTTP Saturday,

    October 6, 12
  82. Saturday, October 6, 12

  83. Saturday, October 6, 12

  84. Questions? • joind.in/6942 • nodephp.org • @reactphp • @igorwesome Saturday,

    October 6, 12
  85. Saturday, October 6, 12