REACT PHP
The NodeJS Challenger
LUKE KYSOW
Software Engineer @Hootsuite
Find me on Twitter @lkysow
What is React
PHP?
Written by Igor Wiedler • @igorwhiletrue
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket, $loo...
What Does PHP
Suck At?
C10K Problem
Websockets
Making lots of concurrent
requests (ex. Web Scraping)
Speed
Why does PHP
suck at solving
these problems?
Why is NodeJS
good at solving
these problems?
A typical web request
<?php
$request = $this->getRequest();
$param = $request->getParameter('param');
// these calls block...
Latency Numbers Every
Programmer Should Know
L1 cache reference 0.5 ns
L2 cache reference 7 ns
Main memory reference 100 n...
A typical web request
<?php
$request = $this->getRequest(); // 1ns
$param = $request->getParameter('param'); //
// 100,000...
Solution:
(╯°□°)╯︵ ┻━┻
Implement Non-Blocking I/O
and Event Loops in PHP!
Aside: Streams
$filePointer=fopen("http://google.com","rb
echostream_get_contents($filePointer,25);
//"<!doctypehtml><html...
Aside: Streams
$filePointer = fopen("file:///tmp/test", "w"
fwrite($filePointer,
"Actual React code coming soon");
Convert this...
<?php
$request = $this->getRequest();
$param = $request->getParameter('param');
// these calls block
$apiR...
// get requests as streams
$apiStream = $api->getStream($param);
$dbStream = $database->getStream($param);
// still blocks...
$apiStream = $api->getStream($param);
$dbStream = $database->getStream($param);
// return asynchronously
$this->retrieveSt...
stream_select
$filePointer = fopen("http://google.com", "r
$readable = [$filePointer];
$write = []; $exc = []; $t = 5;
if (stream_select...
What do we yield control to?
An Event Loop
<?php
while (true) {
$event = $this->getNextEvent();
$event->process();
}
while (true) {
if (stream_select($read, $write, $exc, 1))
if ($read) {
foreach ($readable as $stream) {
// run callback fo...
Non Blocking I/O
+ Event Loops
== Awesome
== React PHP
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$socket->on('connection', function ($conn...
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket);
$ht...
Pipes
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$socket->on('connection', function ...
$waiting = null;
$socket->on('connection',
function ($conn) use (&$waiting) {
if (null === $waiting) {
$waiting = $conn;
$...
React Projects
React In The Industry
Web Scraping with React
High Performance Symfony
TL;DR: You'll get with this approach almost 2.000 requests/s instead of
130 on a large Symfony ap...
Is React PHP a
NodeJS
Challenger?
NO
Fin
Luke Kysow • @lkysow
Please give me your feedback! http://tiny.cc/reactphp
Upcoming SlideShare
Loading in …5
×

React PHP: the NodeJS challenger

6,042 views

Published on

Event looped, asynchronous programming is possible with PHP! Streams, promises, async IO, web sockets -- you can do it all, with React PHP. Presented by Luke Kysow.

Published in: Technology

React PHP: the NodeJS challenger

  1. 1. REACT PHP The NodeJS Challenger LUKE KYSOW Software Engineer @Hootsuite Find me on Twitter @lkysow
  2. 2. What is React PHP? Written by Igor Wiedler • @igorwhiletrue
  3. 3. $loop = ReactEventLoopFactory::create(); $socket = new ReactSocketServer($loop); $http = new ReactHttpServer($socket, $loop) $http->on('request', function ($req, $rep) { $rep->writeHead(); $rep->end("Hello World!n"); }); $socket->listen(8000); $loop->run();
  4. 4. What Does PHP Suck At?
  5. 5. C10K Problem
  6. 6. Websockets
  7. 7. Making lots of concurrent requests (ex. Web Scraping)
  8. 8. Speed
  9. 9. Why does PHP suck at solving these problems?
  10. 10. Why is NodeJS good at solving these problems?
  11. 11. A typical web request <?php $request = $this->getRequest(); $param = $request->getParameter('param'); // these calls block $apiResponse = $api->getSomething($param); $dbResponse = $database->doSomething($param) return new Response($apiResponse, $dbRespons
  12. 12. Latency Numbers Every Programmer Should Know L1 cache reference 0.5 ns L2 cache reference 7 ns Main memory reference 100 ns Send 1K bytes over 1 Gbps network 10,000 ns Read 1 MB sequentially from memory 250,000 ns Round trip within same datacenter 500,000 ns
  13. 13. A typical web request <?php $request = $this->getRequest(); // 1ns $param = $request->getParameter('param'); // // 100,000,000 ns $apiResponse = $api->getSomething($param); // 500,000 ns $dbResponse = $database->doSomething($param) // 1ns return new Response($apiResponse, $dbRespons
  14. 14. Solution: (╯°□°)╯︵ ┻━┻ Implement Non-Blocking I/O and Event Loops in PHP!
  15. 15. Aside: Streams $filePointer=fopen("http://google.com","rb echostream_get_contents($filePointer,25); //"<!doctypehtml><htmlitem" echostream_get_contents($filePointer,25); //"scope=""itemtype="http:/"
  16. 16. Aside: Streams $filePointer = fopen("file:///tmp/test", "w" fwrite($filePointer, "Actual React code coming soon");
  17. 17. Convert this... <?php $request = $this->getRequest(); $param = $request->getParameter('param'); // these calls block $apiResponse = $api->getSomething($param); $dbResponse = $database->doSomething($param) return new Response($apiResponse, $dbRespons
  18. 18. // get requests as streams $apiStream = $api->getStream($param); $dbStream = $database->getStream($param); // still blocks list($apiResp, $dbResp) = $this->retrieveData($apiStream, $dbStream) return new Response($apiResp, $dbResp);
  19. 19. $apiStream = $api->getStream($param); $dbStream = $database->getStream($param); // return asynchronously $this->retrieveStreams($apiStream, $dbStre ->on('streamsReady', function($apiResp, $dbResp) { return new Response($apiResp, $dbRes } );
  20. 20. stream_select
  21. 21. $filePointer = fopen("http://google.com", "r $readable = [$filePointer]; $write = []; $exc = []; $t = 5; if (stream_select($readable, $write, $exc, $ if ($readable) { $googleStream = $readable[0]; echo stream_get_contents($googleStream, // echoes <!doctype html><html item } }
  22. 22. What do we yield control to?
  23. 23. An Event Loop <?php while (true) { $event = $this->getNextEvent(); $event->process(); }
  24. 24. while (true) { if (stream_select($read, $write, $exc, 1)) if ($read) { foreach ($readable as $stream) { // run callback for that stream } } if ($write) { foreach ($writable as $stream) { // run callback for that stream }} } }
  25. 25. Non Blocking I/O + Event Loops == Awesome
  26. 26. == React PHP
  27. 27. $loop = ReactEventLoopFactory::create(); $socket = new ReactSocketServer($loop); $socket->on('connection', function ($conn) { $conn->write("Hello there!n"); $conn->write("Don't say anything...n"); $conn->on('data', function ($data) use ($conn) { $conn->close(); }); }); $socket->listen(1337); $loop->run()
  28. 28. $loop = ReactEventLoopFactory::create(); $socket = new ReactSocketServer($loop); $http = new ReactHttpServer($socket); $http->on('request', function ($request, $response) { echo "I see a client!n"; $response->writeHead(200, ['Content-Type' => 'text/plain']); $response->end("Hello World!n"); }); $socket->listen(1337); $loop->run();
  29. 29. Pipes $loop = ReactEventLoopFactory::create(); $socket = new ReactSocketServer($loop); $socket->on('connection', function ($conn) { $conn->pipe($conn); } ); $socket->listen(1337); $loop->run();
  30. 30. $waiting = null; $socket->on('connection', function ($conn) use (&$waiting) { if (null === $waiting) { $waiting = $conn; $conn->write("Wait for a partner.n"); } else { $conn->write("Connected!"); $waiting->write("Connected!"); $conn->pipe($waiting)->pipe($conn); $waiting = null; }});
  31. 31. React Projects React In The Industry Web Scraping with React
  32. 32. High Performance Symfony TL;DR: You'll get with this approach almost 2.000 requests/s instead of 130 on a large Symfony app. Even if you have an "opcode cache" you have to declare classes, instantiate your objects, read your caches, etc for every single request. As you can surely imagine this is very time consuming and far away from being a perfect setup for high performance. http://marcjschmidt.de/blog/2014/02/08/php-high-performance.html
  33. 33. Is React PHP a NodeJS Challenger?
  34. 34. NO
  35. 35. Fin Luke Kysow • @lkysow Please give me your feedback! http://tiny.cc/reactphp

×