PHPStreamServer is a high performance event-loop based process manager, TCP, and UDP server written in PHP.
With a built-in PSR-7 HTTP server you can easily integrate any PSR-7 compatible framework with it in no time.
The built-in HTTP server is memory efficient no matter how large your HTTP requests and responses you operate are.
PHPStreamServer is supports TLS encryption and the ability to implement custom protocols.
- Supervisor;
- Workers lifecycle management (reload by TTL, max memory, max requests, on exception, on each request);
- PSR-7 HTTP server;
- Unix based OS (no windows support);
- php-posix and php-pcntl extensions;
- php-uv extension is not required, but highly recommended for better performance.
$ composer require luzrain/phpstreamserver
Here is example of simple http server.
// server.php
use Luzrain\PHPStreamServer\Exception\HttpException;
use Luzrain\PHPStreamServer\Listener;
use Luzrain\PHPStreamServer\Server;
use Luzrain\PHPStreamServer\Server\Connection\ConnectionInterface;
use Luzrain\PHPStreamServer\Server\Http\Psr7\Response;
use Luzrain\PHPStreamServer\Server\Protocols\Http;
use Luzrain\PHPStreamServer\WorkerProcess;
use Psr\Http\Message\ServerRequestInterface;
$server = new Server();
$server->addWorkers(new WorkerProcess(
name: 'HTTP Server',
onStart: function (WorkerProcess $worker) {
$worker->startListener(new Listener(
listen: 'tcp://0.0.0.0:80',
protocol: new Http(),
onMessage: function (ConnectionInterface $connection, ServerRequestInterface $data): void {
$response = match ($data->getUri()->getPath()) {
'/' => new Response(body: 'Hello world'),
'/ping' => new Response(body: 'pong'),
default => throw HttpException::createNotFoundException(),
};
$connection->send($response);
},
));
},
));
exit($server->run());
$ php server.php start