⚠️ Archived 2025-08-17Use at your own risk.
Serve PSR-7 middleware applications from React.
$ composer require "react/http:^0.5@dev" phly/react2psr7react2psr7 currently requires features from the upcoming 0.5 release of react/http. Since that version is not yet released, you need to specify it manually when installing to force Composer to allow a development release.
The following demonstrates creating an HTTP server using React, and using an Expressive application to handle incoming requests.
<?phpuseReact\EventLoop\Factory;
useReact\Http\ServerasHttpServer;
useReact\Socket\ServerasSocket;
useReact2Psr7\ReactRequestHandler;
useZend\Expressive\Application;
require_once'vendor/autoload.php';
$loop = Factory::create();
$socket = newSocket($loop);
$http = newHttpServer($socket);
$container = require'config/container.php';
$http->on('request', newReactRequestHandler($container->get(Application::class)));
// Listen on all ports; omit second argument to restrict to localhost.$socket->listen(1337, '0.0.0.0');
$loop->run();This package also provides middleware for serving static files; this can be useful when running React as a web server, to allow serving CSS, JavaScript, and images.
The following demonstrates using Stratigility to build a middleware pipeline that consumes both the static files middleware as well as an Expressive application in order to provide a full-fledged web server.
<?phpuseReact\EventLoop\Factory;
useReact\Http\ServerasHttpServer;
useReact\Socket\ServerasSocket;
useReact2Psr7\ReactRequestHandler;
useReact2Psr7\StaticFiles;
useZend\Expressive\Application;
useZend\Stratigility\MiddlewarePipe;
require_once'vendor/autoload.php';
$loop = Factory::create();
$socket = newSocket($loop);
$http = newHttpServer($socket);
$container = require'config/container.php';
$pipeline = newMiddlewarePipe();
$pipeline->pipe(newStaticFiles());
$pipeline->pipe($container->get(Application::class));
$http->on('request', newReactRequestHandler($pipeline));
// Listen on all ports; omit second argument to restrict to localhost.$socket->listen(1337, '0.0.0.0');
$loop->run();(Note: you could also pipe the static files middleware into your Expressive application.)