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
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.4k
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
200
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
120
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
380
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
140
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.8k
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
210
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
130
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
160
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
560
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
150
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
A Tale of Four Properties
chriscoyier
163
24k
A better future with KSS
kneath
240
18k
Building AI with AI
inesmontani
PRO
1
1.1k
Side Projects
sachag
455
43k
Evolving SEO for Evolving Search Engines
ryanjones
0
220
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
Designing Experiences People Love
moore
143
24k
4 Signs Your Business is Dying
shpigford
187
22k
Designing for Performance
lara
611
70k
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