Your SlideShare is downloading. ×
React PHP: the NodeJS challenger
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

React PHP: the NodeJS challenger

2,497
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.

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

0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,497
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
24
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. REACT PHP The NodeJS Challenger LUKE KYSOW Software Engineer @Hootsuite Find me on Twitter @lkysow
  • 2. What is React PHP? Written by Igor Wiedler • @igorwhiletrue
  • 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. What Does PHP Suck At?
  • 5. C10K Problem
  • 6. Websockets
  • 7. Making lots of concurrent requests (ex. Web Scraping)
  • 8. Speed
  • 9. Why does PHP suck at solving these problems?
  • 10. Why is NodeJS good at solving these problems?
  • 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. 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. 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. Solution: (╯°□°)╯︵ ┻━┻ Implement Non-Blocking I/O and Event Loops in PHP!
  • 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. Aside: Streams $filePointer = fopen("file:///tmp/test", "w" fwrite($filePointer, "Actual React code coming soon");
  • 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. // 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. $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. stream_select
  • 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. What do we yield control to?
  • 23. An Event Loop <?php while (true) { $event = $this->getNextEvent(); $event->process(); }
  • 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. Non Blocking I/O + Event Loops == Awesome
  • 26. == React PHP
  • 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. $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. Pipes $loop = ReactEventLoopFactory::create(); $socket = new ReactSocketServer($loop); $socket->on('connection', function ($conn) { $conn->pipe($conn); } ); $socket->listen(1337); $loop->run();
  • 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. React Projects React In The Industry Web Scraping with React
  • 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. Is React PHP a NodeJS Challenger?
  • 34. NO
  • 35. Fin Luke Kysow • @lkysow Please give me your feedback! http://tiny.cc/reactphp

×