Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
React (confoo)
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Igor Wiedler
March 01, 2013
Programming
830
7
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
React (confoo)
Igor Wiedler
March 01, 2013
More Decks by Igor Wiedler
See All by Igor Wiedler
Redis Bedtime Stories
igorw
1
360
Wide Event Analytics (LISA19)
igorw
4
940
a day in the life of a request
igorw
0
170
production: an owner's manual
igorw
0
190
The Power of 2
igorw
0
340
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
480
The Moral Character of Software
igorw
1
310
interdisciplinary computing (domcode)
igorw
0
320
miniKanren (clojure berlin)
igorw
1
330
Other Decks in Programming
See All in Programming
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
920
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
750
はてなアカウント基盤 State of the Union
cockscomb
0
680
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
260
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
13
6.3k
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
120
Vite+ Unified Toolchain for the Web
naokihaba
0
340
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
930
Oxcを導入して開発体験が向上した話
yug1224
4
340
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.7k
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.4k
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
180
Featured
See All Featured
Building an army of robots
kneath
306
46k
Speed Design
sergeychernyshev
33
1.9k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
310
HDC tutorial
michielstock
2
720
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
240
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
150
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Producing Creativity
orderedlist
PRO
348
40k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
220
Being A Developer After 40
akosma
91
590k
Docker and Python
trallard
47
3.9k
Transcript
None
Y U NO PHP?
@igorwesome
None
None
None
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');
None
$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); }
$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");
Non-blocking I/O
C10K
Calculations are fast, I/O is slow.
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
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
None
stream_select
$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) { ... } } }
$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
None
None
Event-driven, non-blocking I/O with PHP.
Demo
$loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new
React\Http\Server($socket); $http->on('request', function ($req, $rep) { $rep->writeHead(); $rep->end("Hello World!\n"); }); $socket->listen(8080); $loop->run();
$loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $socket->on('connection', function ($conn)
{ $conn->pipe($conn); }); $socket->listen(4000); $loop->run();
None
# composer.json { "require": { "react/http": "0.2.*" } }
$ composer install
Philosophy
Architecture
Événement EventEmitter for PHP
$emitter = new Evenement\EventEmitter(); $emitter->on('data', function ($data) { echo $data;
}); $emitter->emit('data', [$data]);
EventLoop Stream Socket Http
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); ... });
EventLoop $loop->addPeriodicTimer(5, function () { $memory = memory_get_usage() / 1024;
$formatted = number_format($memory, 3).'K'; echo "Current memory usage: {$formatted}\n"; });
EventLoop $loop->run();
EventLoop backends stream_select libevent libev upcoming libuv
ReadableStream isReadable() pause() resume() close() Events data, end, error, close
Stream
ReadableStream isReadable() pause() resume() close() Events data, end, error, close
Stream
WritableStream isWritable() write($data) end($data) close() Events drain, error, close, pipe
Stream
WritableStream isWritable() write($data) end($data) close() Events drain, error, close, pipe
Stream
Stream $source = new React\Stream\Stream(fopen('omg.txt', 'r'), $loop); $dest = new
React\Stream\Stream(fopen('wtf.txt', 'w'), $loop); $source->pipe($dest);
Stream $a->pipe($b)->pipe($c); a | b | c
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);
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);
EventLoop Stream Socket Http
Http HttpClient DNS
HttpClient Http DNS Stomp Whois
HttpClient Http DNS Stomp Whois WebSocket DNode SOCKS IRC
None
$foo->get(function ($bar) { $bar->get(function ($baz) { $baz->get(function ($bazinga) { $bazinga->get(function
() { throw new FuckItException(); }); }); }); });
async API (escaping callback hell)
React/Async
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(); } ]);
React/Promise
$dns ->resolve('igor.io') ->then(function ($ip) { echo "Host: $ip\n"; });
$promise ->then('doStuff') ->then('doMoreStuff') ->then('explosion') ->then(null, function ($e) { echo "[Error]
{$e->getMessage()}\n"; });
Deferred Promise Resolver Consumer Producer then() resolve() reject()
Streams
Readable Writable
Readable Writable Through
Readable Writable Through Composite
Buffered Sink
Bi-directional
conn logger parser $conn->pipe($inputLogger)->pipe($parser);
parser client $parser->on('message', function ($message) use ($client) { $client->say('pong'); });
client conn logger $client->pipe($outputLogger)->pipe($conn);
conn parser client conn
Houston we have a blocking call
Inter-process communication
React/ZMQ
$context = new React\ZMQ\Context($loop); $push = $context->getSocket(ZMQ::SOCKET_PUSH); $push->connect('tcp://127.0.0.1:5555'); $push->send('hello');
React/Stomp Predis/Async DNode-PHP
RealtimeWeb™
Ratchet
gifsockets!!!11
None
None
None
Questions? • joind.in/7969 • reactphp.org • @reactphp • @igorwesome
Questions? • joind.in/7969 • reactphp.org • @reactphp • @igorwesome
Http Server Worker Client Worker Worker Client Client HTTP