phpdot / routing-rt
Real-time routing for WebSocket and SSE — extends phpdot/routing.
Requires
- php: >=8.5
- phpdot/contracts: ^0.1
- phpdot/realtime: ^0.1
- phpdot/routing: ^0.1
- psr/http-message: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.94
- phpdot/container: ^0.1
- phpdot/http: ^0.1
- phpstan/phpstan: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
README
Real-time routing for WebSocket and SSE, layered on phpdot/routing.
RouterRT extends the HTTP Router and adds ws() and sse() alongside get()/post(), so the same
router — and the same groups, middleware, names, and parameter constraints — serves HTTP, WebSocket, and
Server-Sent Events. WebSocket channels dispatch through phpdot/realtime;
SSE streams through an SSEWriter.
Table of Contents
Requirements
| Requirement | Constraint |
|---|---|
| PHP | >= 8.5 |
phpdot/contracts |
^0.1 |
phpdot/realtime |
^0.1 |
phpdot/routing |
^0.1 |
psr/http-message |
^2.0 |
phpdot/container is a dev-only suggestion — the #[Singleton] attribute on RouterRT is inert until a
phpdot application reflects it.
Installation
composer require phpdot/routing-rt
Usage
use PHPdot\Routing\RouterRT\RouterRT; $app = new RouterRT($container, $responseFactory); // HTTP — exactly the phpdot/routing API $app->get('/chat/{room}', [ChatPageController::class, 'show']); // WebSocket $app->ws('/chat/{room}', [ChatController::class, 'index']); // SSE $app->sse('/dashboard/{id:int}', [DashboardController::class, 'stream']);
Groups, middleware, names, and where constraints all work across the three, and the same path can serve
both HTTP and WebSocket without collision:
$app->ws('/chat/{room}', [ChatController::class, 'index']) ->name('ws.chat') ->middleware(AuthMiddleware::class);
list() returns every route (HTTP, WS, SSE); compile() compiles them all.
Architecture
RouterRT extends the HTTP Router, so HTTP dispatch is unchanged. ws() registers a WsRoute whose
frames are dispatched through the phpdot/realtime Hub (with Ack for request/response over the socket);
sse() registers a handler that streams through an SSEWriter. Because it is a Router, one binding
can make RouterRT the application's router everywhere.
graph TD
ROUTER["phpdot/routing Router<br/><br/>HTTP: get / post / group / middleware"]
RT["RouterRT extends Router<br/><br/>adds ws() and sse()"]
WS["WsRoute → phpdot/realtime Hub<br/><br/>WebSocket channels, Ack req/response"]
SSE["SSEController → SSEWriter<br/><br/>Server-Sent Events stream"]
ROUTER --> RT
RT --> WS
RT --> SSE
Loading
Testing
composer install composer test # PHPUnit composer analyse # PHPStan, level max + strict rules composer cs-check # PHP-CS-Fixer composer check # All three
License
MIT.
This repository is a read-only mirror, generated by CI from phpdot/monorepo. Pull requests and issues belong in the monorepo.