1. Go to this page and download the library: Download phpdot/server library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpdot / server example snippets
use PHPdot\Http\Factory\ResponseFactory;
use PHPdot\Server\Config\HttpServerConfig;
use PHPdot\Server\Config\ServerConfig;
use PHPdot\Server\Http\HttpServer;
use PHPdot\Server\Server;
$factory = new ResponseFactory();
$server = new Server(new ServerConfig(workerNum: 4));
$server->attach(new HttpServer($factory, new HttpServerConfig(port: 8080)));
$server->serve($handler); // any PSR-15 RequestHandlerInterface — blocks on the event loop
use PHPdot\Server\Config\TcpServerConfig;
use PHPdot\Server\Tcp\TcpServer;
$server->attach(new HttpServer($factory, $httpConfig));
$server->attach(new TcpServer(new TcpServerConfig(port: 9501)));
$server->serve($handler); // handler also implements TcpHandlerInterface
$registry->send($fd, $data); // raw write to one connection
$registry->pushWs($fd, $frame); // WebSocket push, established sockets only
$registry->pingWs($fd); // WebSocket PING control frame (heartbeats)
$registry->broadcastWs($frame); // push to every open WebSocket
$registry->broadcast($data, $tcpPort); // raw-TCP broadcast, never touches HTTP/WS sockets
$registry->disconnect($fd, 4401, 'bye'); // WebSocket close handshake; no-op on dead fds
$registry->close($fd); // close a connection
$registry->exists($fd); // liveness check
$registry->info($fd); // Swoole client info
$registry->list($startFd, $count); // iterate connected fds
final class Bootstrap implements OnWorkerStartInterface, OnBeforeShutdownInterface
{
public function onWorkerStart(\Swoole\Server $server, int $workerId): void { /* warm caches */ }
public function onBeforeShutdown(\Swoole\Server $server): void { /* flush */ }
}
$server->events()->subscribe(new Bootstrap());
use PHPdot\Server\Attribute\ServerListener;
use PHPdot\Server\Event\WorkerStarted;
#[ServerListener]
final class WarmCache
{
public function __construct(private readonly CacheWarmer $warmer) {}
public function __invoke(WorkerStarted $event): void
{
$this->warmer->warm(); // DI-built, in-worker, post-fork
}
}
$factory->discover([$vendorPhpdot, $appSourceDir]); // then $factory->create()
$server->onTask(fn (mixed $data) => heavyWork($data)); // runs in a task worker
$server->onFinish(fn (mixed $result) => log($result)); // back on the originating worker
// dispatch (from within a request):
$dispatcher->task($payload, onFinish: fn ($r) => handle($r)); // fire-and-callback
$results = $dispatcher->taskCo([$a, $b, $c], timeout: 0.5); // dispatch + coroutine-await all
$server->reload(); // graceful worker reload (new code, no dropped connections)
$server->reload(onlyReloadTaskWorker: true);
$server->stop($workerId); // stop one worker (it respawns)
$server->shutdown(); // stop the whole server
$server->ensurePortsAvailable(); // fail-fast port check before serve()