The document discusses asynchronous programming in PHP using non-blocking I/O and event loops. It covers using asynchronous techniques like ReactPHP to scrape web pages concurrently without blocking. Promises and streams are also discussed as ways to handle asynchronous operations and pass data between components. Finally, messaging techniques like websockets and WAMP are presented as ways to build real-time applications.
6. request time
www.amazon.com: 0.80137
www.reddit.com: 0.21584
www.hackernews.com: 1.80921
www.google.com: 0.29365
www.yahoo.com: 1.39217
!
Total time in seconds: 4.51274
8. I/O is slow.
Send 1K bytes over Gbps network 0.01 ms
Read 4K randomly from SSD* 0.15 ms
Read 1 MB sequentially from memory 0.25 ms
Round trip within same datacenter 0.5 ms
Read 1 MB sequentially from SSD* 1 ms
Disk seek 10 ms
Read 1MB sequentially from disk 20 ms
Send packet CA->Netherlands->CA 150 ms
* Assuming ~1GB/sec SSD source: https://gist.github.com/jboner/2841832
9. page scraper
var urls = ['http://www.amazon.com', ...];
!
for(var i in urls) {
http.get(urls[i], function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
!
response.on('end', function () {
// do something with data
});
});
}
10. request time
www.amazon.com: 0.75559
www.reddit.com: 0.33153
www.hackernews.com: 0.57661
www.google.com: 0.48226
www.yahoo.com: 0.23333
!
Total time: 0.76421
21. Promises/A
states
“A promise represents the eventual value returned
from the single completion of an operation.”
pending, fulfilled, rejected
“once fulfilled or rejected the promise’s
value shall not be changed”
http://wiki.commonjs.org/wiki/Promises/A
22. working with a Promise
$loop = ReactEventLoopFactory::create();
$factory = new ReactDnsResolverFactory();
$dns = $factory->create('8.8.8.8', $loop);
!
$dns
->resolve('github.com')
->then(function ($ip) {
echo "Host: $ipn";
}
);
$loop->run();
Deferred
23. working with Promiseall()
$file1 = new file('test_file.txt', "r", $loop);
$file2 = new file('test_file2.txt', "r", $loop);
$promises = array(
$file1->read(),
$file2->read()
);
!
Promiseall($promises)->then(function($v){});
Promiserace($promises)->then(function($v){});
Promisesome($promises, 4)->then(function($v){});
30. Server Sent Events
supported by all major browsers
var source = new EventSource('stream.php');
• automatically re-connects
• uni-directional
• send arbitrary events
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
39. Ratchet
$socketServer = new ReactSocketServer($loop);
$socketServer->listen(8080, '0.0.0.0');
$wampServer = new IoServer(
new HttpServer(
new WsServer(
new WampServer(
$myWampApp
)
)
),
$socketServer,
$loop
);
onCall($conn, $id, $topic, $params)
onPublish($conn, $topic, $event)
onSubscribe($conn, $topic)
onUnsubscribe($conn, $topic)
40. Ratchet
wamp connection
$conn->callResult($id,$this->playerData);
$conn->callError($id, $topic, 'You are not allowed to make calls');
topic
$topic->broadcast($event, array($conn->WAMP->sessionId));