Pushing the limits of PHP with ReactPHP – why ReactPHP is awesome and why you should care (IPC17)

Pushing the limits of PHP with ReactPHP – why ReactPHP is awesome and why you should care (IPC17)

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.

D1b6700884ac0ae368918ad171bb6a75?s=128

Christian Lück

June 01, 2017
Tweet

Transcript

  1. 2.

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

    - Enter React - Core components - Examples and demo time - Conclusions 2
  2. 12.

    Who are you? 12 now that you know me… -

    PHP developers? - architecs / engineers?
  3. 13.

    Who are you? 13 now that you know me… -

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

    14

  5. 18.

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

    stack - Request-Response-Cycle 18 Apache Client PHP MySQL
  6. 19.

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

    stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 19 Apache Client PHP MySQL
  7. 20.

    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 Apache Client FPM MySQL PHP PHP
  8. 21.

    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 nginx Client FPM MySQL PHP PHP
  9. 22.

    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 nginx Client FPM memcache PHP PHP MySQL
  10. 27.

    Knock knock! 2017! - Separation of concerns (Frontend↔Backend) - HTTP

    APIs (RESTful) - Integration with 3rd parties - Live-Data (ticker) - CLI tools Who’s there? 27
  11. 29.
  12. 35.
  13. 46.

    I/O is everywhere third party HTTP APIs (RESTful, SOAP, you

    name it…) mysql, postgres filesystem I/O (session files) 46
  14. 47.

    I/O is everywhere third party HTTP APIs (RESTful, SOAP, you

    name it…) mysql, postgres filesystem I/O (session files) redis, memcache 47
  15. 53.
  16. 54.

    This is React 54 Start multiple I/O operations (non-blocking) Get

    notified when something happens (react) Don’t waste time waiting
  17. 56.

    What React is not - React is not black magic

    / vodoo - Does not make your code faster magically 56
  18. 57.

    What React is not - React is not black magic

    / vodoo - Does not make your code faster magically - React is not a framework - Indepentent components (libraries) 57
  19. 58.

    What React is not - React is not black magic

    / vodoo - Does not make your code faster magically - React is not a framework - Indepentent components (libraries) - React is not a replacement for your favorite framework - Symfony is here to stay 58
  20. 59.

    What React is not - React is not black magic

    / vodoo - Does not make your code faster magically - React is not a framework - Indepentent components (libraries) - React is not a replacement for your favorite framework - Symfony is here to stay - React is not the new buzz - React is here to stay as well (started in 2012) 59
  21. 63.

    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
  22. 64.

    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();
  23. 66.

    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’; });
  24. 67.

    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);
  25. 68.

    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
  26. 69.

    Streams - interfaces, events and listeners: 69 $dest->write(‘hello’); $source->on(‘data’, function

    ($data) { var_dump($data); }); $source->on(‘close’, function () { echo ‘stream closed’; });
  27. 70.

    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);
  28. 71.

    Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 71
  29. 72.

    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’);
  30. 75.

    Socket server 75 react/socket - THE canonical chat example -

    broadcast all incoming msgs - run example server - connect via telnet: $ telnet ipc.clue.engineering 6001
  31. 76.

    76

  32. 77.

    HTTP client 77 clue/buzz-react - Simple HTTP requests - inspired

    by kriswallsmith/buzz - PSR-7 compatible
  33. 78.

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

    by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast…
  34. 79.

    HTTP client 79 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
  35. 80.

    Packagist API clue/packagist-api-react - get information about any Composer package

    - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 80
  36. 81.

    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 - see its examples $ php examples/search.php
  37. 82.

    Docker client clue/docker-react - Run apps in isolated containers -

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 82
  38. 83.

    Docker client clue/docker-react - Run apps in isolated containers -

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 83 - see its promise examples $ php examples/info.php - see its streaming examples $ php examples/benchmark-exec.php
  39. 87.

    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.) 87
  40. 90.
  41. 91.

    5k requests/s 91 this is a local single core benchmark!

    dual core i3 => 10k requests/s 36M requests/h
  42. 92.

    Server sent events clue/sse-react - Server sent events (SSE) -

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 92
  43. 93.

    Server sent events clue/sse-react - Server sent events (SSE) -

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

    94

  45. 95.

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

    flow between browser and server - better browser support 95
  46. 96.

    Redis client 96 clue/redis-react - Redis is a fast in-memory

    DB - very simple commands - very simple protocol - pipelined, Promise-based
  47. 97.

    Redis client 97 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
  48. 101.

    Redis server 101 clue/php-redis-server - Official Redis is written in

    C - Reimplementation is pure PHP - Very simple to add commands
  49. 102.

    Redis server 102 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…
  50. 103.

    Redis server 103 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 - PHP server: ~50k OP/s
  51. 107.

    Redis framework 107 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
  52. 108.

    108

  53. 109.

    UPnP clue/ssdp-react - Univeral Plug and Play (UPnP) - Simple

    Service Discovery Protocol (SSDP) - dicover UPnP-enabled devices - printers - routers - multi media devices 109
  54. 110.

    UPnP clue/ssdp-react - Univeral Plug and Play (UPnP) - Simple

    Service Discovery Protocol (SSDP) - dicover UPnP-enabled devices - printers - routers - multi media devices 110 - see its examples $ php examples/search.php
  55. 112.

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

    Promise-based 112 - 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
  56. 113.

    113

  57. 114.

    114

  58. 115.

    115

  59. 118.

    Conclusions 118 - React is a real deal and here

    to stay - React is stable and production ready - predictable releases - but possibly feature-incomplete (check open issues)
  60. 119.

    Conclusions 119 - React is a real deal and here

    to stay - React is stable and production ready - predictable releases - but possibly feature-incomplete (check open issues) - React does not make your code faster magically
  61. 120.

    Conclusions 120 - React is a real deal and here

    to stay - React is stable and production ready - predictable releases - but possibly feature-incomplete (check open issues) - React does not make your code faster magically - Consider using React whenever you’re having to wait - Consider using React whenever you’re accessing the network
  62. 124.

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

    - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp 124
  63. 125.

    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 125
  64. 126.

    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? 126
  65. 129.

    Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 129
  66. 130.

    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 130
  67. 131.

    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 131
  68. 132.

    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 132 Pay attention: - PDO, mysql etc. - file system access - network access - third-party APIs
  69. 134.

    Integration with traditional environments 134 integrating async into sync is

    easy - just run the loop until you’re done - see clue/block-react
  70. 135.

    Integration with traditional environments 135 integrating async into sync is

    easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard
  71. 136.

    Integration with traditional environments 136 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