Pushing the limits of PHP with React PHP

Pushing the limits of PHP with React PHP

It's 2016 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 React PHP 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

May 12, 2016
Tweet

Transcript

  1. pushing the limits with React PHP why React PHP is

    awesome and why YOU should care
  2. Agenda - Hello! - PHP, the web of the ‘90s?

    - Enter React - Core concepts - Examples and demo time - Conclusions 2
  3. Hello! 3

  4. $ whoami - Christian Lück 4

  5. $ whoami - Christian Lück → Christian Lueck 5

  6. $ whoami - Christian Lück → Christian Lueck → @clue

    6
  7. $ whoami - Christian Lück → Christian Lueck → @clue

    → @another_clue 7
  8. $ whoami - Christian Lück → Christian Lueck → @clue

    → @another_clue - Team Coach / Software Engineering, working professionally for 10 years - Masters degree in Information Systems Engineering 8
  9. $ whoami - Christian Lück → Christian Lueck → @clue

    → @another_clue - Team Coach / Software Engineering, working professionally for 10 years - Masters degree in Information Systems Engineering - Started coding 15 years ago and could never stop :) - Passionate about pushing the limits! - Open source contributor and maintainer (React, Docker, Graphs and more) 9
  10. $ whoami - Christian Lück → Christian Lueck → @clue

    → @another_clue - Team Coach / Software Engineering, working professionally for 10 years - Masters degree in Information Systems Engineering - Started coding 15 years ago and could never stop :) - Passionate about pushing the limits! - Open source contributor and maintainer (React, Docker, Graphs and more) - Quit my job in April to focus on thrilling things ahead… - Freelance job: Bigger momentum, higher impact, spread the word! 10
  11. Who are you? 11 now that you know me…

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

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

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

    PHP developers? - architecs / engineers? - know React? - know the other React?
  15. PHP, the web of the ‘90s? 15

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

    stack - Request-Response-Cycle Apache Client PHP MySQL 16
  17. PHP and the web of the ‘90s - traditional LAMP

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

    stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 18 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP
  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 Apache Client FPM MySQL PHP PHP nginx Client FPM MySQL PHP PHP
  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 nginx Client FPM MySQL PHP PHP nginx Client FPM memcache PHP PHP MySQL
  21. PHP may not be pretty… but it gets the job

    done! 21
  22. Knock knock! Who’s there? 22

  23. Knock knock! 2016! Who’s there? 23

  24. Knock knock! 2016! - Separation of concerns (Frontend) - HTTP

    APIs (RESTful) - Integration with 3rd parties - Live-Data (ticker) - CLI tools Who’s there? 24
  25. node js? 25

  26. node js! 26

  27. Enter React! 27

  28. What is React? 28

  29. What is React? non-blocking I/O 29

  30. What is React? non-blocking I/O event-driven 30

  31. What is React? non-blocking I/O event-driven async, not parallel 31

  32. 32

  33. 100% 33

  34. 100% pure PHP 34

  35. 100% pure PHP no extensions 35

  36. 100% pure PHP no extensions no magic 36

  37. What does that even mean?! 37

  38. non-blocking I/O 38

  39. non-blocking I/O 39 - Calculations are fast – I/O is

    slow
  40. non-blocking I/O 40 - Calculations are fast – I/O is

    slow Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832
  41. non-blocking I/O 41 - Calculations are fast – I/O is

    slow - So why wait on I/O? (blocking) Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832
  42. non-blocking I/O 42 - Calculations are fast – I/O is

    slow - So why wait on I/O? (blocking) - Start multiple I/O operations (non-blocking) - get notified when something happens - hello async! Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832
  43. non-blocking I/O 43 - Calculations are fast – I/O is

    slow - So why wait on I/O? (blocking) - Start multiple I/O operations (non-blocking) - get notified when something happens - hello async! - single thread only so far - Multithreading + multi cores Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832
  44. What React is not 44

  45. What React is not - React is not a framework

    - Indepentent components (libraries) 45
  46. What React is not - React is not a framework

    - Indepentent components (libraries) - React is not black magic / vodoo - Does not make your code faster magically 46
  47. What React is not - React is not a framework

    - Indepentent components (libraries) - React is not black magic / vodoo - Does not make your code faster magically - React is not a replacement for your favorite framework - Symfony is here to stay 47
  48. What React is not - React is not a framework

    - Indepentent components (libraries) - React is not black magic / vodoo - Does not make your code faster magically - 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) 48
  49. React core concepts 49

  50. React Core concepts - Event loop (reactor) - Promises -

    Streams - Avoid blocking 50
  51. Event loop Consumers - THE core, low-level component 51

  52. 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 52
  53. 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 53 Implementors - Reactor pattern (hence the name)
  54. 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 54 Implementors - Reactor pattern (hence the name) - wait for stream resources to become - readable - writable - start timers - once - periodic
  55. Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 55
  56. Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) - no more imperative code flow $a = doA(); $b = doB(); process($a, $b); 56
  57. Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) - no more imperative code flow $a = doA(); $b = doB(); process($a, $b); - instead (tell, don’t ask) $a->then($fulfilled = null, $rejected = null); $a->then(‘process’, ‘var_dump’); 57
  58. 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) 58
  59. 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) - interfaces, events and listeners: $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); 59
  60. Avoid blocking! - The loop must not be blocked 60

  61. Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 61
  62. 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 62
  63. 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 63
  64. 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 64 These should be avoided: - PDO, mysql etc. - file system access - network access - third-party APIs
  65. Examples and demo time! 65

  66. Socket server 66 react/socket - THE canonical chat example -

    broadcast all incoming msgs
  67. Socket server 67 react/socket - THE canonical chat example -

    broadcast all incoming msgs - run example server - connect via telnet: $ telnet IP 8000
  68. HTTP client 68 clue/buzz-react - Simple HTTP requests - inspired

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

    by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast…
  70. HTTP client 70 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
  71. Packagist API clue/packagist-api-react - get information about any Composer package

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

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

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

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 74 - see its promise examples $ php examples/info.php - see its streaming examples $ php examples/benchmark-exec.php - throughput during talk: ~ 720 MiB/s, Docker maxed out, PHP not
  75. HTTP server react/http - Pure PHP, with no additional webserver

    - Under active development 75
  76. HTTP server react/http - Pure PHP, with no additional webserver

    - Under active development - Lots of third-party integrations with traditional frameworks (symfony, slim, silex, PIMF etc.) 76
  77. Server sent events clue/sse-react - Server sent events (SSE) -

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

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 78 - see examples connecting to initial chat $ php examples/chat-server.php - open browser: http://localhost:8001/
  79. Websocket server cboden/ratchet - Async WebSocket server - bidirectional data

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

    DB - very simple commands - very simple protocol - pipelined, Promise-based
  81. Redis client 81 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
  82. Redis server 82

  83. Redis server 83 - Official Redis is written in C

  84. Redis server 84 clue/php-redis-server - Official Redis is written in

    C - Reimplementation is pure PHP
  85. Redis server 85 clue/php-redis-server - Official Redis is written in

    C - Reimplementation is pure PHP - Very simple to add commands
  86. Redis server 86 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…
  87. Redis server 87 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
  88. UPnP clue/ssdp-react - Univeral Plug and Play (UPnP) - Simple

    Service Discovery Protocol (SSDP) - dicover UPnP-enabled devices - printers - routers - multi media devices 88
  89. UPnP clue/ssdp-react - Univeral Plug and Play (UPnP) - Simple

    Service Discovery Protocol (SSDP) - dicover UPnP-enabled devices - printers - routers - multi media devices 89 - see its examples $ php examples/search.php
  90. Zenity clue/zenity-react - PHP desktop GUI applications - very simple,

    Promise-based 90
  91. Zenity clue/zenity-react - PHP desktop GUI applications - very simple,

    Promise-based 91 - see its simple examples $ php examples/dialog.php - see its more realistic examples $ php examples/menu.php $ php examples/progress-*.php
  92. many, MANY more third-party projects: https://github.com/reactphp/react/wiki/Users 92

  93. Conclusions 93

  94. Conclusions 94 - React is a real deal and here

    to stay - React is stable and production ready - predictable releases - but possibly feature-incomplete (check open issues)
  95. Conclusions 95 - 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
  96. Conclusions 96 - 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
  97. PHP is faster than you probably thought 97

  98. Integration with traditional environments 98 integrating async into sync is

    easy
  99. Integration with traditional environments 99 integrating async into sync is

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

    easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard
  101. Integration with traditional environments 101 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
  102. Need help? Want to help? - check each component’s README

    - check open issues 102
  103. Need help? Want to help? - check each component’s README

    - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp 103
  104. 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 104
  105. 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? 105
  106. // thank you! $loop->end(); 106 http://lueck.tv/