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 (phpbnl13)
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Igor Wiedler
January 26, 2013
Programming
1.3k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
React (phpbnl13)
Igor Wiedler
January 26, 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
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
130
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
200
JavaDoc 再入門
nagise
1
420
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
190
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
110
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.3k
Inside Stream API
skrb
1
790
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.6k
The NotImplementedError Problem in Ruby
koic
1
950
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
310
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Fireside Chat
paigeccino
42
4k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
330
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
210
Docker and Python
trallard
47
3.9k
Color Theory Basics | Prateek | Gurzu
gurzu
0
370
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
360
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
380
エンジニアに許された特別な時間の終わり
watany
107
250k
Optimizing for Happiness
mojombo
378
71k
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, $loop); $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/7805 • reactphp.org • @reactphp • @igorwesome
Questions? • joind.in/7805 • reactphp.org • @reactphp • @igorwesome
Http Server Worker Client Worker Worker Client Client HTTP