Reactive programming ... with PHP

Jonathan VUILLEMIN

2017-02

Reactive programming ?

Quick definition

To put it simply, it is a matter of listening to events from multiple sources to react to them asynchronously.

 

The result produced by the application, for example an html page, is not constructed by following a procedure in a well defined order, but by reacting to the different types of events that can influence this result.

 

It is particularly suitable when the application is often waiting for external calls (database, REST API),

or actions of a user (click on a button, message in a chat).

Reactor design pattern

The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs.

 

 

The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated event handler.

Reactor design pattern

  • Service request: Any resource that can provide input to or consume output from the system.
  • Synchronous event demultiplexer: Uses an event loop to block on all resources. When it is possible to start a synchronous operation wihout blocking on a resource , the demultiplexer sends the resource to the dispatcher.
  • Dispatcher: Handles registering and unregistering of event handlers. Dispatches resources from the demultiplexer to the associated event handler.
  • Event handler: An application defined event handler and its associated resource.

ReactPHP event loop = single thread

Benefits

  • Optimal usage of CPU.
  • Can handle more requests with same hardware.
  • Can scale above C10K limit. (https://en.wikipedia.org/wiki/C10k_problem)
  • ​Separates application specific code from the reactor implementation, which means that application components can be divided into modular, reusable parts.

Disavantages

  • Asynchronous event based code base makes it difficult to understand and structure code ... but Promise library can help.
  • Debugging code will be more difficult since stack trace begins from the call-back instead of start of request.
  • Memory leaks for long time running scripts

Can be used for ...

Paralleling external calls (for non-blocking IO):

Let a php script run as an http or socket server:

Less talking, more code...

I/O Operations: large multiple downloads

Blocking I/O download (regular_download.php)

Downloading file node-v0.6.18.tar.gz ... download OK, size : 9.786 MB
Downloading file php-5.5.15.tar.gz ... download OK, size : 16.301 MB
Downloading file node-v0.11.9-sunos-x64.tar.gz ... download OK, size : 6.954 MB
Downloading file node-v0.11.9-sunos-x86.tar.gz ... download OK, size : 6.567 MB
Execution time: 12.392359972%

Parallel react loop download (parallel_download.php)

##### Loop tick #####
node-v0.6.18.tar.gz: 1.311 MB
php-5.5.15.tar.gz: 1.911 MB
node-v0.11.9-sunos-x64.tar.gz: 2.382 MB
node-v0.11.9-sunos-x86.tar.gz: 0.610 MB
##### Loop tick #####
node-v0.6.18.tar.gz: 2.606 MB
php-5.5.15.tar.gz: 3.402 MB
node-v0.11.9-sunos-x64.tar.gz: 5.606 MB
node-v0.11.9-sunos-x86.tar.gz: 1.492 MB
Finished downloading node-v0.11.9-sunos-x64.tar.gz
##### Loop tick #####
node-v0.6.18.tar.gz: 4.486 MB
php-5.5.15.tar.gz: 4.949 MB
node-v0.11.9-sunos-x86.tar.gz: 2.759 MB
##### Loop tick #####
node-v0.6.18.tar.gz: 7.001 MB
php-5.5.15.tar.gz: 7.542 MB
node-v0.11.9-sunos-x86.tar.gz: 4.337 MB
##### Loop tick #####
node-v0.6.18.tar.gz: 9.681 MB
php-5.5.15.tar.gz: 10.152 MB
node-v0.11.9-sunos-x86.tar.gz: 5.296 MB
Finished downloading node-v0.6.18.tar.gz
Finished downloading node-v0.11.9-sunos-x86.tar.gz
##### Loop tick #####
php-5.5.15.tar.gz: 13.643 MB
Finished downloading php-5.5.15.tar.gz
##### Loop tick #####
Execution time: 7.9870839118958% 

Websocket chat application

Ratchet: WebSockets for PHP, based on ReactPHP

 

Ratchet is a loosely coupled PHP library providing developers with tools to create real time,

bi-directional applications between clients and servers over WebSockets.

This is not your Grandfather's Internet.

Example: server.php

Questions ?

 

Thanks !

Reactive programming

By Jonathan VUILLEMIN

Reactive programming

Reactive programming with PHP

  • 1,088