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.2(3w ago)015MITPHPPHP &gt;=8.4

Since Apr 14Pushed 2mo 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 3w ago

READMEChangelogDependencies (17)Versions (11)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

Maintenance89

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity58

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 ~8 days

Recently: every ~19 days

Total

10

Last Release

23d ago

Major Versions

v1.1.0 → v2.0.02026-04-16

PHP version history (2 changes)v1.0.0PHP &gt;=8.3

v2.5.1PHP &gt;=8.4

### 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

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

7.9k1.1B4.1k](/packages/guzzlehttp-psr7)[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.2k543.5M2.7k](/packages/aws-aws-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k656.1k46](/packages/neuron-core-neuron-ai)[phrity/websocket

WebSocket client and server

2164.9M41](/packages/phrity-websocket)[hyperf/hyperf

A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.

6.9k3.3k2](/packages/hyperf-hyperf)[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

36789.4k2](/packages/telnyx-telnyx-php)

PHPackages © 2026

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