This document discusses making PHP applications more reactive and asynchronous using ReactPHP principles. It covers:
1. The benefits of using a reactive programming approach with ReactPHP including increased performance from non-blocking I/O.
2. How to build a reactive Symfony application using ReactPHP by splitting the application into asynchronous framework, server, and domain layers.
3. The Symfony Async Kernel and ReactPHP Symfony server packages that facilitate building reactive Symfony applications on ReactPHP.
4. Benchmarks showing the ReactPHP approach outperforming traditional PHP and PHP-PM implementations for concurrent requests.
Domain + I/O
Wehave our domain,
where all happens. Our
classes, our chosen
libraries and our business
logic. We can place I/O like
Redis calls in this group
14
Goods
- Already workswith N
ReactPHP servers
- This N is variable
- Static content
- Easy to install and configure
- Adapter for Symfony kernel
built-in
- No need of Nginx
PHP-PM
Bads ?
29
30.
Goods
- Already worksonly with N
ReactPHP servers
- This N is variable
- Static content
- Easy to install and configure
- Adapter for Symfony kernel
built-in
- No need of Nginx
PHP-PM
Bads
- Each ReactPHP server act as
a regular sync server. One
request after the last
- 10 servers + 10 long time
requests = fail
- Code too complex to work
with
30
ReactPHP libraries
Some ofthem are part of the core of the application
◎ /http - HTTPS server for ReactPHP
◎ /promises - Promises implementation
◎ /event-loop - EventLoop implementation
◎ /stream - PHP Streams wrapper for ReactPHP
◎ /socket - Socket server and connections for ReactPHP
49
50.
ReactPHP libraries
And someof them just side libraries to use on top of it
◎ Redis client
◎ Mysql client
◎ AMQP client
◎ Filesystem
◎ HTTP Client
◎ DNS Resolver
◎ Cache
50
“
Event listeners canreturn a
Promise as part of your domain.
Check inside and outside the
Promise effects
71
72.
/**
* Handle getResponse.
*
* @param GetResponseEvent $event
*
* @return PromiseInterface
*/
public function handleGetResponsePromiseA(GetResponseEvent $event)
{
$promise = (new FulfilledPromise())
->then(function () use ($event) {
// This line is executed eventually after the previous listener
// promise is fulfilled
$event->setResponse(new Response('A'));
});
// This line is executed before the first event listener promise is
// fulfilled
return $promise;
}
72
73.
“
Each I/O operationmust be
performed by using Reactive
libraries (filesystem, Redis, Http
requests, Mysql, RabbitMQ...)
73
74.
“
A single syncI/O operation will
make your whole application sync
74
Next steps
The futureof PHP will be async, so you better be ready for
that
◎ Work for a Promises PSR. Let frameworks depend on
an interface instead of an implementation
◎ Start doing some serious stuff with Promises in PHP
◎ We should start seeing some live projects on top of
ReactPHP - Apisearch soon!
87
88.
Next steps
◎ Somelibraries should be adapted to Reactive
programming, or we should create new (DBAL, Logger,
Elasticsearch client…)
◎ Start talking a little bit more about this, because a
language is not it’s code, but how far his community
can go.
88