PHPackages                             phpdot/routing-rt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. phpdot/routing-rt

ActiveLibrary[HTTP &amp; Networking](/categories/http)

phpdot/routing-rt
=================

Real-time routing for WebSocket and SSE — extends phpdot/routing

v2.5.0(1mo ago)014MITPHPPHP &gt;=8.3

Since Apr 14Pushed 1mo agoCompare

[ Source](https://github.com/phpdot/routing-rt)[ Packagist](https://packagist.org/packages/phpdot/routing-rt)[ RSS](/packages/phpdot-routing-rt/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (8)Versions (9)Used By (0)

phpdot/routing-rt
=================

[](#phpdotrouting-rt)

Real-time routing for WebSocket and SSE. Extends [phpdot/routing](https://github.com/phpdot/routing) — same API, same features, two extra methods.

Requirements
------------

[](#requirements)

- PHP 8.3+
- phpdot/routing ^1.1

Installation
------------

[](#installation)

```
composer require phpdot/routing-rt
```

Usage
-----

[](#usage)

```
use PHPdot\Routing\RouterRT\RouterRT;

$app = new RouterRT($container, $responseFactory);

// HTTP — same as Router
$app->get('/chat/{room}', [ChatPageController::class, 'show']);
$app->post('/users', [UserController::class, 'store']);

// WebSocket
$app->ws('/chat/{room}', [ChatController::class, 'index']);

// SSE
$app->sse('/dashboard/{id:int}', [DashboardController::class, 'stream']);

// Everything works: groups, middleware, names, where, expose
$app->group('/api', function ($group) use ($app) {
    $app->ws('/chat/{room}', [ChatController::class, 'index'])
        ->name('ws.chat')
        ->middleware(AuthMiddleware::class);

    $app->sse('/feed', [FeedController::class, 'stream'])
        ->name('sse.feed');
});

// list() returns all routes — HTTP, WS, SSE
$app->list();

// compile() compiles everything
$app->compile();
```

Same path can serve both HTTP and WebSocket without collision.

### Inside the phpdot framework

[](#inside-the-phpdot-framework)

`RouterRT` carries `#[Singleton]`, so when used with `phpdot/package` it's auto-wired by the container — no manual `new RouterRT(...)` needed. To make `RouterRT` the default for everywhere your app asks for a `Router`, override the binding in your application boot:

```
use PHPdot\Routing\Router;
use PHPdot\Routing\RouterRT\RouterRT;

$builder->register(
    Router::class,
    new ScopedDefinition(scope: Scope::SINGLETON, implementation: RouterRT::class),
);
```

Anywhere your code asks for `Router::class`, the container hands back a `RouterRT` (which extends `Router`). All HTTP route registrations work unchanged; `->ws()` and `->sse()` become available.

When served through `phpdot/server-swoole`, the server auto-detects `RouterRT` (via its `WebSocketHandlerInterface` and `SseHandlerInterface` markers) and wires Swoole's `onOpen` / `onMessage` / `onClose` event hooks to the router automatically. No additional configuration needed.

Contracts
---------

[](#contracts)

### WebSocketController

[](#websocketcontroller)

```
use PHPdot\Routing\RouterRT\Contracts\WebSocketController;
use PHPdot\Routing\RouterRT\WebSocketConnection;
use PHPdot\Routing\RouterRT\Frame;

final class ChatController implements WebSocketController
{
    public function __construct(
        private WebSocketConnection $conn,
    ) {}

    public function onOpen(): void
    {
        $room = $this->conn->param('room');
        $this->conn->send(['event' => 'joined', 'room' => $room]);
    }

    public function onMessage(Frame $frame): void
    {
        $this->conn->send(['echo' => $frame->data]);
    }

    public function onClose(int $code, string $reason): void {}
}
```

### SSEController

[](#ssecontroller)

```
use PHPdot\Routing\RouterRT\Contracts\SSEController;
use PHPdot\Routing\RouterRT\SSEWriter;

final class DashboardController implements SSEController
{
    public function stream(SSEWriter $writer): void
    {
        $writer->retry(3000);

        while (!$writer->isClosed()) {
            $writer->event('metrics', ['cpu' => 42, 'mem' => 78]);
            sleep(1);
        }
    }
}
```

WebSocketConnection
-------------------

[](#websocketconnection)

Server-agnostic WebSocket connection. No Swoole dependency — fully testable.

```
$conn->id();                          // File descriptor
$conn->send('text');                  // Send text frame
$conn->send(['key' => 'value']);      // Auto JSON-encode
$conn->sendBinary($bytes);           // Send binary frame
$conn->close(1000, 'bye');           // Close connection
$conn->param('room');                // Route parameter
$conn->params();                     // All route parameters
$conn->attribute('user_id');         // Request attribute (from middleware)
$conn->request();                    // Original upgrade request
```

SSEWriter
---------

[](#ssewriter)

SSE protocol formatting with closed-state tracking.

```
$writer->event('update', ['id' => 1]);           // Named event
$writer->event('update', 'payload', '42');        // With ID
$writer->data('unnamed payload');                  // Unnamed data
$writer->comment('keep-alive');                    // Comment
$writer->retry(5000);                              // Reconnection interval (ms)
$writer->close();                                  // Close stream
$writer->isClosed();                               // Check state
```

Frame
-----

[](#frame)

```
use PHPdot\Routing\RouterRT\Frame;
use PHPdot\Routing\RouterRT\Opcode;

$frame = new Frame($data, Opcode::Text);
$frame->data;    // string
$frame->opcode;  // Opcode::Text | Opcode::Binary
```

What Is NOT In This Package
---------------------------

[](#what-is-not-in-this-package)

- Rooms, broadcasting, presence — `phpdot/channel`
- WebSocketConnection management — framework wiring
- Swoole event registration — framework wiring

Testing
-------

[](#testing)

```
composer check
```

License
-------

[](#license)

MIT

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance92

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

8

Last Release

37d ago

Major Versions

v1.1.0 → v2.0.02026-04-16

### Community

Maintainers

![](https://www.gravatar.com/avatar/62e82421bda4b5d6ba9a47ba6d88caca060dcd0d1a2862f351f3a97657385db0?d=identicon)[phpdot](/maintainers/phpdot)

---

Top Contributors

[![phpdot](https://avatars.githubusercontent.com/u/252500?v=4)](https://github.com/phpdot "phpdot (8 commits)")

---

Tags

websocketreal-timeroutingsseswoole

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpdot-routing-rt/health.svg)

```
[![Health](https://phpackages.com/badges/phpdot-routing-rt/health.svg)](https://phpackages.com/packages/phpdot-routing-rt)
```

###  Alternatives

[hhxsv5/laravel-s

🚀 LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.

3.9k686.6k13](/packages/hhxsv5-laravel-s)[elephantio/elephant.io

Send events to a socket.io server through PHP

136801.9k7](/packages/elephantio-elephantio)[denis660/laravel-centrifugo

Centrifugo broadcaster for laravel

119182.2k](/packages/denis660-laravel-centrifugo)[vinelab/minion

A Simple WAMP (Web Application Messaging Protocol) server and command line tool

1276.5k](/packages/vinelab-minion)[hyperf/websocket-server

A websocket server library for Hyperf.

12513.1k29](/packages/hyperf-websocket-server)[hosmelq/sse

A PHP library for consuming Server-Sent Events (SSE) streams with WHATWG compliance.

145.0k1](/packages/hosmelq-sse)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
