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

Getting started with ReactPHP – Pushing real-ti...

Getting started with ReactPHP – Pushing real-time data to the browser (PHPBenelux20)

Think about PHP for a few seconds… What came to mind? It’s very likely you thought about your average product catalog, a blogging platform, or how the platform is inferior to things like Node.js. But wait, it’s 2020! What if I told you PHP’s huge ecosystem has way more to offer and PHP is not inferior at all to its evil cousin Node.js?

In this talk 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 interesting examples and live demos learn why what sounds crazy at first might soon be a valuable addition in your toolbox.

You’re already familiar with PHP and want to learn what ReactPHP is all about? Then this talk is for you! We will start from scratch and see what it takes to build an application that pushes data from your command line to your browser in real-time. You’re invited to join the interactive demos or lean back and learn more about why an event-driven approach might be the next big thing in making your application faster and more responsive.

---

These slides were used as part of a presentation at @phpbenelux. The full presentation took 60min with basic project setup, getting started with ReactPHP's core components and eventually implementing an HTTP server using Server-Sent Events (EventSource).

Resulting source code can be found here: https://gist.github.com/clue/c426bcee9c7767157085d6b881204998

Christian Lück

January 25, 2020
Tweet

More Decks by Christian Lück

Other Decks in Programming

Transcript

  1. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data 4
  2. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets 5
  3. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises 6
  4. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises - real time data 7
  5. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises - real time data - Streaming HTTP 8
  6. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises - real time data - Streaming HTTP - Chat bots 9
  7. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises - real time data - Streaming HTTP - Chat bots - interactive CLI tools 10
  8. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises - real time data - Streaming HTTP - Chat bots - interactive CLI tools - desktop GUI 11
  9. @another_clue Agenda 12 - Hello! - Introduction to async PHP

    with ReactPHP - event loops - streaming data - sockets - promises - real time data - Streaming HTTP - Chat bots - interactive CLI tools - desktop GUI - Conclusions
  10. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises - conclusions 17
  11. @another_clue Agenda - Hello! - Introduction to async PHP with

    ReactPHP - event loops - streaming data - sockets - promises - conclusions 18 examples and demo time!
  12. @another_clue Who are you? 30 now that you know me…

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

    - PHP developers? - architecs / engineers? - know React?
  14. @another_clue PHP and the web of the ‘90s traditional LAMP

    stack Request-Response-Cycle 36 Apache Client PHP MySQL
  15. @another_clue PHP and the web of the ‘90s traditional LAMP

    stack Request-Response-Cycle PHP is too slow? 37 Apache Client PHP MySQL
  16. @another_clue PHP and the web of the ‘90s traditional LAMP

    stack Request-Response-Cycle PHP is too slow? 38 Apache Client MySQL FPM PHP PHP
  17. @another_clue PHP and the web of the ‘90s traditional LAMP

    stack Request-Response-Cycle PHP is too slow? 39 nginx Client MySQL FPM PHP PHP
  18. @another_clue PHP and the web of the ‘90s traditional LAMP

    stack Request-Response-Cycle PHP is too slow? 40 nginx Client FPM PHP PHP memcache MySQL
  19. @another_clue Knock knock! 2020! - Separation of concerns (Frontend↔Backend) -

    HTTP APIs (RESTful) - Integration with 3rd parties - Live-Data (ticker) - CLI tools Who’s there? 45
  20. @another_clue Knock knock! 2020! - Separation of concerns (Frontend↔Backend) -

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

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

    you name it…) mysql, postgres filesystem I/O (session files) redis, memcache 80
  23. @another_clue This is React 86 Start multiple I/O operations (non-blocking)

    Get notified when something happens (react) Don’t waste time waiting
  24. @another_clue What React is not React is not black magic

    / vodoo React is not a framework 89
  25. @another_clue What React is not React is not black magic

    / vodoo React is not a framework React is not the new buzz 90
  26. @another_clue 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 94
  27. @another_clue 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 95
  28. @another_clue Event loop Implementors - Reactor pattern (hence the name)

    - start timers - once - periodic - ticks - plus wait for stream resources to become - readable - writable 100
  29. @another_clue 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) 102
  30. @another_clue Sockets 109 - Streams, but over the network -

    servers listen - clients connect - Focus on TCP/IP (UDP etc. also possible)
  31. @another_clue Sockets 110 - Streams, but over the network -

    servers listen - clients connect - Focus on TCP/IP (UDP etc. also possible)
  32. @another_clue Sockets 111 - Streams, but over the network -

    servers listen - clients connect - Focus on TCP/IP (UDP etc. also possible)
  33. @another_clue Sockets 112 - Streams, but over the network -

    servers listen - clients connect - Focus on TCP/IP (UDP etc. also possible)
  34. @another_clue Sockets in practice - implementation detail - higher level

    protocols (HTTP anybody) - higher level concepts (RESTful etc.) 116
  35. @another_clue Sockets in practice - implementation detail - higher level

    protocols (HTTP anybody) - higher level concepts (RESTful etc.) - higher level APIs (SDKs, API clients etc.) 117
  36. @another_clue Sockets in practice - implementation detail - higher level

    protocols (HTTP anybody) - higher level concepts (RESTful etc.) - higher level APIs (SDKs, API clients etc.) - Concepts still apply, details are hidden 118
  37. @another_clue Promises - Placeholder for a single future result -

    Possible states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 120
  38. @another_clue Promises in practice - Everywhere! - react/socket - clue/buzz-react

    - clue/packagist-react - clue/redis-react - react/mysql - … 128
  39. @another_clue HTTP in a gist 132 GET / HTTP/1.1 HTTP/1.1

    200 OK Content-Type: text/plain hello world!
  40. @another_clue HTTP in a gist 133 GET / HTTP/1.1 HTTP/1.1

    200 OK Content-Type: text/plain hello world!
  41. @another_clue HTTP streaming 136 - HTTP request/response semantics - streaming

    responses (download huge files) - streaming requests (upload huge files)
  42. @another_clue HTTP streaming 137 - HTTP request/response semantics - streaming

    responses (download huge files) - streaming requests (upload huge files) - Still request/response semantics
  43. @another_clue HTTP streaming 138 - HTTP request/response semantics - streaming

    responses (download huge files) - streaming requests (upload huge files) - Still request/response semantics - Long-polling (Comet) anyone?
  44. @another_clue HTTP streaming 139 - HTTP request/response semantics - streaming

    responses (download huge files) - streaming requests (upload huge files) - Still request/response semantics - Long-polling (Comet) anyone?
  45. @another_clue Server-Sent Events in a gist 143 - Normal Request/Response

    - Client API for streaming access HTTP/1.1 200 OK Content-Type: text/event-stream
  46. @another_clue Server-Sent Events in a gist 144 - Normal Request/Response

    - Client API for streaming access HTTP/1.1 200 OK Content-Type: text/event-stream data: hello
  47. @another_clue Server-Sent Events in a gist 145 - Normal Request/Response

    - Client API for streaming access HTTP/1.1 200 OK Content-Type: text/event-stream data: hello data: world
  48. @another_clue Server-Sent Events in a gist 146 - Normal Request/Response

    - Client API for streaming access HTTP/1.1 200 OK Content-Type: text/event-stream data: hello data: world
  49. @another_clue PubSub 152 - Publish/Subscribe - common pattern for distributed

    system - message broker in between Publisher Redis Subscriber
  50. @another_clue PubSub 153 - Publish/Subscribe - common pattern for distributed

    system - message broker in between - horizontal scalability - independent components Publisher Redis Subscriber
  51. @another_clue PubSub 154 - Publish/Subscribe - common pattern for distributed

    system - message broker in between - horizontal scalability - independent components Publisher Redis Subscriber Publisher Subscriber Publisher Subscriber
  52. @another_clue PubSub 155 - Publish/Subscribe - common pattern for distributed

    system - message broker in between - horizontal scalability - independent components Publisher Redis Subscriber Publisher Subscriber Publisher Subscriber
  53. @another_clue PubSub 156 - Publish/Subscribe - common pattern for distributed

    system - message broker in between - horizontal scalability - independent components Publisher Redis Subscriber Publisher Subscriber Publisher Subscriber
  54. @another_clue PubSub 157 - Publish/Subscribe - common pattern for distributed

    system - message broker in between - horizontal scalability - independent components Publisher Redis Subscriber Publisher Subscriber Publisher Subscriber
  55. @another_clue YES 163 But you might be missing the point…

    Can I use ReactPHP to make my website 1000x faster?
  56. @another_clue ReactPHP 170 a real deal and here to stay

    stable & production ready *awesome*
  57. @another_clue Integration with traditional environments 185 integrating async into sync

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

    is easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard
  59. @another_clue Integration with traditional environments 187 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
  60. @another_clue Avoid blocking! - The loop must not be blocked

    - Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 189
  61. @another_clue 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 190
  62. @another_clue 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 191
  63. @another_clue 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 192 Pay attention: - PDO, mysql etc. - file system access - network access - third-party APIs