Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pushing the limits with ReactPHP (ScotlandPHP17)

Pushing the limits with ReactPHP (ScotlandPHP17)

It’s 2017 and times have changed – yet PHP is still most often associated with your average product catalogue or blogging platform. In this talk you will learn that PHP’s huge ecosystem has way more to offer and PHP is not inferior at all to its evil cousin Node.js.

You will learn about the core concepts of async PHP and why you too should care about ReactPHP being a real thing. The talk has a strong focus on sparking the idea that PHP can be way faster and more versatile than you probably thought. Bring along an open mind and through lots of examples and demos learn why what sounds crazy at first might soon be a valuable addition in your toolbox.

Christian Lück

November 04, 2017
Tweet

More Decks by Christian Lück

Other Decks in Programming

Transcript

  1. Agenda - Hello! - PHP, the web of the ‘90s?

    - Enter React - Core components - Examples and demo time - Conclusions 2
  2. Who are you? 13 now that you know me… -

    PHP developers? - architecs / engineers?
  3. Who are you? 14 now that you know me… -

    PHP developers? - architecs / engineers? - know React?
  4. 15

  5. PHP and the web of the ‘90s - traditional LAMP

    stack - Request-Response-Cycle - PHP is too slow? 19 Apache Client PHP MySQL
  6. PHP and the web of the ‘90s - traditional LAMP

    stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 20 Apache Client PHP MySQL
  7. PHP and the web of the ‘90s - traditional LAMP

    stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 21 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP
  8. PHP and the web of the ‘90s - traditional LAMP

    stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 22 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP nginx Client FPM MySQL PHP PHP
  9. PHP and the web of the ‘90s - traditional LAMP

    stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 23 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP nginx Client FPM MySQL PHP PHP nginx Client FPM memcache PHP PHP MySQL
  10. Knock knock! 2017! - Separation of concerns (Frontend↔Backend) - HTTP

    APIs (RESTful) - Integration with 3rd parties - Live-Data (ticker) - CLI tools Who’s there? 28
  11. I/O is everywhere third party HTTP APIs (RESTful, SOAP, you

    name it…) mysql, postgres filesystem I/O (session files) 47
  12. I/O is everywhere third party HTTP APIs (RESTful, SOAP, you

    name it…) mysql, postgres filesystem I/O (session files) redis, memcache 48
  13. This is React 55 Start multiple I/O operations (non-blocking) Get

    notified when something happens (react) Don’t waste time waiting
  14. What React is not React is not black magic /

    vodoo React is not a framework 58
  15. What React is not React is not black magic /

    vodoo React is not a framework React is not the new buzz 59
  16. Event loop Consumers - THE core, low-level component - Create

    an instance - Just use the Factory - Additional extensions for bigger payloads - something inbetween… - just pass the $loop around - Start running - keeps running forever - unless stopped or done 63
  17. Event loop Consumers - THE core, low-level component - Create

    an instance - Just use the Factory - Additional extensions for bigger payloads - something inbetween… - just pass the $loop around - Start running - keeps running forever - unless stopped or done 64 $loop = Factory::create(); // something inbetween // pass the $loop around to all components $loop->run();
  18. Event loop Implementors - Reactor pattern (hence the name) -

    start timers - once - periodic - ticks 66 $loop->addTimer(0.5, function () { echo ‘world’; }); $loop->addTimer(0.3, function () { echo ‘hello’; });
  19. Event loop Implementors - Reactor pattern (hence the name) -

    start timers - once - periodic - ticks - wait for stream resources to become - readable - writable 67 $loop->addTimer(0.5, function () { echo ‘world’; }); $loop->addTimer(0.3, function () { echo ‘hello’; }); $loop->addReadStream($stream, $fn); $loop->addWriteStream($stream, $fn);
  20. Streams - Process large strings in chunks as they happen

    (think downloads) - Types - Readable (e.g. STDIN pipe) - Writable (e.g. STDOUT pipe) - Duplex (e.g. TCP/IP connection) 68
  21. Streams - interfaces, events and listeners: 69 $dest->write(‘hello’); $source->on(‘data’, function

    ($data) { var_dump($data); }); $source->on(‘close’, function () { echo ‘stream closed’; });
  22. Streams - interfaces, events and listeners: 70 $dest->write(‘hello’); $source->on(‘data’, function

    ($data) { var_dump($data); }); $source->on(‘close’, function () { echo ‘stream closed’; }); $source->pipe($gunzip)->pipe($badwords)->pipe($dest);
  23. Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 71
  24. Promises - no more imperative code flow - instead (tell,

    don’t ask) 72 $a->then($fulfilled = null, $rejected = null); $a->then(‘process’); $a->then(‘process’, ‘var_dump’);
  25. Socket server 76 react/socket - THE canonical chat example -

    broadcast all incoming msgs - run example server - connect via telnet: $ telnet clue.engineering 6001
  26. 77

  27. HTTP client 78 clue/buzz-react - Simple HTTP requests - inspired

    by kriswallsmith/buzz - PSR-7 compatible
  28. HTTP client 79 clue/buzz-react - Simple HTTP requests - inspired

    by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast…
  29. HTTP client 80 clue/buzz-react - Simple HTTP requests - inspired

    by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast… - benchmarks in following slides about clue/docker-react
  30. Packagist API clue/packagist-api-react - get information about any Composer package

    - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 81
  31. Packagist API clue/packagist-api-react - get information about any Composer package

    - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 82 - see its examples $ php examples/search.php
  32. Docker client clue/docker-react - Run apps in isolated containers -

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 83
  33. Docker client clue/docker-react - Run apps in isolated containers -

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 84 - see its promise examples $ php examples/info.php - see its streaming examples $ php examples/benchmark-exec.php
  34. HTTP server react/http - Pure PHP, with no additional webserver

    - standard PSR-7 interfaces - Lots of third-party integrations with traditional frameworks (symfony, slim, silex, PIMF etc.) 89
  35. 5k requests/s 94 this is a local single core benchmark!

    dual core i3 => 10k requests/s 36M requests/h
  36. Server sent events clue/sse-react - Server sent events (SSE) -

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 95
  37. Server sent events clue/sse-react - Server sent events (SSE) -

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 96 - see examples connecting to initial chat $ php examples/chat-server.php - open browser: http://clue.engineering:7000/
  38. 97

  39. Websocket server cboden/ratchet - Async WebSocket server - bidirectional data

    flow between browser and server - better browser support 98
  40. Redis client 99 clue/redis-react - Redis is a fast in-memory

    DB - very simple commands - very simple protocol - pipelined, Promise-based
  41. Redis client 100 clue/redis-react - Redis is a fast in-memory

    DB - very simple commands - very simple protocol - pipelined, Promise-based - see its examples $ php examples/incr.php
  42. Redis server 104 clue/php-redis-server - Official Redis is written in

    C - Reimplementation is pure PHP - Very simple to add commands
  43. Redis server 105 clue/php-redis-server - Official Redis is written in

    C - Reimplementation is pure PHP - Very simple to add commands - How fast could PHP possibly be? Let’s see…
  44. Redis server 106 clue/php-redis-server - Official Redis is written in

    C - Reimplementation is pure PHP - Very simple to add commands - How fast could PHP possibly be? Let’s see… - see its bin $ php bin/redis-server.php - test via clue/redis-react - test via official redis CLI - run official redis benchmark during talk: - official server: ~90k OP/s
  45. Redis framework 111 clue/php-redis-framework - Development preview - Very simple

    to add custom commands - run example server $ php examples/11-beer.php - connect via telnet: $ telnet IP 9000
  46. 112

  47. 114

  48. Zenity clue/zenity-react - PHP desktop GUI applications - very simple,

    Promise-based 116 - see its simple examples $ php examples/01-dialog.php - see its more realistic examples $ php examples/06-menu.php $ php examples/03-progress-pulsate.php $ php examples/03-progress-random.php
  49. 117

  50. 118

  51. 119

  52. Need help? Want to help? - check each component’s README

    - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp 133
  53. Need help? Want to help? - check each component’s README

    - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp - Talk to me 134
  54. Need help? Want to help? - check each component’s README

    - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp - Talk to me Did I mention I’m available? 135
  55. Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 139
  56. Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered - Alternatives - Single result: Promises - Evented: Streams 140
  57. Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered - Alternatives - Single result: Promises - Evented: Streams - Need a blocking function? - Fork off! - Use IPC 141
  58. Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered - Alternatives - Single result: Promises - Evented: Streams - Need a blocking function? - Fork off! - Use IPC 142 Pay attention: - PDO, mysql etc. - file system access - network access - third-party APIs
  59. Integration with traditional environments 144 integrating async into sync is

    easy - just run the loop until you’re done - see clue/block-react
  60. Integration with traditional environments 145 integrating async into sync is

    easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard
  61. Integration with traditional environments 146 integrating async into sync is

    easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard - often requires async rewrite - consider forking instead