kinetis / broadcasting
Real-time broadcasting for Kinetis through Pusher-protocol servers (Soketi, Laravel Reverb, Pusher), a Mercure hub, or Centrifugo - attribute-based private/presence channel authorization and non-blocking drivers over kinetis/revolt-http-client.
Requires
- php: >=8.4 <8.6
- firebase/php-jwt: ^7.1.0
- kinetis/framework: ^1.33.1
- kinetis/revolt-http-client: ^1.9.4
- psr/http-message: ^2.0
Requires (Dev)
- infection/infection: ^0.35.15
- phpstan/phpstan: ^2.2.8
- phpunit/phpunit: ^13.3.3
- vimeo/psalm: ^7.24.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
kinetis/broadcasting
Real-time broadcasting for Kinetis through Pusher-protocol servers, Mercure, or Centrifugo
Part of Kinetis, a non-blocking PHP framework for API-first applications, developed in the kinetis-dev/kinetis monorepo.
Three drivers — the Pusher Channels protocol (Soketi, Laravel Reverb,
Pusher itself), a Mercure hub (standalone or FrankenPHP's embedded one),
and Centrifugo v6 — each publishing non-blocking over
kinetis/revolt-http-client.
use Kinetis\Broadcasting\Broadcaster; final readonly class OrderUpdated implements \Kinetis\Broadcasting\ShouldBroadcast { public function __construct(private string $orderId, private string $status) {} public function broadcastOn(): array { return ["private-orders.{$this->orderId}"]; } public function broadcastAs(): string { return 'order.updated'; } public function broadcastWith(): array { return ['status' => $this->status]; } } // From a controller or a #[Listener] method, both constructor-inject Broadcaster: $broadcaster->event(new OrderUpdated($orderId, 'shipped'));
A private or presence channel authorizes through one attribute:
use Kinetis\Broadcasting\Attributes\BroadcastChannel; use Kinetis\Http\CurrentUserInterface; final class OrderChannels { // OrderRepository is your own; this package resolves the class // through the container like any other service. public function __construct(private OrderRepository $orders) {} #[BroadcastChannel('orders.{orderId}')] public function authorize(CurrentUserInterface $user, string $orderId): bool { return $this->orders->belongsTo($orderId, $user->id()); } }
POST /broadcasting/auth — the endpoint pusher-js, Laravel Echo
(Pusher or Mercure connector) and centrifuge-js call before subscribing
— is discovered and wired with zero registration, and answers in the
bound driver's own protocol.
Provides
Installing this package auto-registers, via extra.kinetis:
- A container binding for
Kinetis\Broadcasting\BroadcasterInterface, selected byBROADCAST_DRIVER:"null", the default — a silent no-op — or"pusher","mercure"or"centrifugo", each requiring its own keys. Validated and built at worker boot, not lazily — a misconfigured driver fails before the first request, not on whichever one happens to broadcast first. Kinetis\Broadcasting\BroadcastChannelRegistry, discovering every#[BroadcastChannel]-attributed method anywhere under your own PSR-4 roots.POST /broadcasting/authand, for Centrifugo,POST /broadcasting/connection-token— real Kinetis routes (Kinetis\Broadcasting\Http\BroadcastAuthController), discovered the same way anyKinetis\Http\Routing\RouteDiscovery-found controller is. A route the bound driver does not offer answers404.- The
broadcastingmiddleware group those routes reference, withKinetis\Broadcasting\Http\BroadcastOriginMiddlewareas its permanent member. Join it with#[AsMiddlewareGroup('broadcasting')]— a thin subclass ofkinetis/auth'sBearerAuthMiddlewareorkinetis/auth-jwt'sJwtAuthMiddlewareis enough — to authenticate these routes and nothing else, and an authorizer takingCurrentUserInterfacereceives the identity it publishes.
Nothing else — Kinetis\Broadcasting\Broadcaster (the service your own
code constructor-injects) autowires from the bound
BroadcasterInterface with no binding of its own.
Configuration
BROADCAST_DRIVER=pusher
BROADCAST_APP_ID=your-app-id
BROADCAST_KEY=your-key
BROADCAST_SECRET=your-secret
BROADCAST_HOST=soketi.example.com
BROADCAST_PORT=6001
BROADCAST_TLS=false
BROADCAST_TIMEOUT=2.0
BROADCAST_ALLOWED_ORIGINS=https://app.example
| Key | Default | Purpose |
|---|---|---|
BROADCAST_DRIVER |
null |
null, pusher, mercure or centrifugo. |
BROADCAST_APP_ID |
(required for pusher) |
The app id both sides agree on. |
BROADCAST_KEY |
(required for pusher) |
The public key. |
BROADCAST_SECRET |
(required for pusher) |
Never sent to a browser. |
BROADCAST_HOST |
api.pusherapp.com |
The broker's host. |
BROADCAST_PORT |
443 |
The broker's port. |
BROADCAST_TLS |
true |
Whether to connect over TLS. |
BROADCAST_TIMEOUT |
30.0 |
The whole budget for one publish, in seconds, for every driver; must be above zero. |
BROADCAST_MERCURE_URL |
(required for mercure) |
The hub URL this application publishes to. |
BROADCAST_MERCURE_PUBLIC_URL |
BROADCAST_MERCURE_URL |
The hub URL browsers open. |
BROADCAST_MERCURE_JWT_SECRET |
(required for mercure) |
The hub's HS256 key, at least 32 bytes. |
BROADCAST_MERCURE_JWT_ISSUER |
(required for mercure) |
The hub's trusted issuer. |
BROADCAST_MERCURE_COOKIE_NAME |
__Secure-mercure_access_token |
The hub's subscriber cookie name. |
BROADCAST_MERCURE_TOKEN_TTL |
300 |
Seconds a subscriber token and its cookie live. |
BROADCAST_CENTRIFUGO_URL |
(required for centrifugo) |
The server API base URL. |
BROADCAST_CENTRIFUGO_API_KEY |
(required for centrifugo) |
Sent as X-API-Key. |
BROADCAST_CENTRIFUGO_TOKEN_SECRET |
(required for centrifugo) |
The HS256 token key, at least 32 bytes. |
BROADCAST_CENTRIFUGO_TOKEN_TTL |
300 |
Seconds a connection or subscription token lives. |
BROADCAST_ALLOWED_ORIGINS |
(empty) | Extra exact Origin values the routes' own guard admits. The request's own origin, and a request sending no Origin at all, pass without it. A cross-origin browser request must also be allowed by the app's global CorsMiddleware. |
BROADCAST_CHANNEL_DISCOVERY_PATHS |
(unset) | Comma-separated sub-paths (relative to each PSR-4 base directory) restricting the #[BroadcastChannel] scan, for a large application that wants a bounded scan. |
Every driver key is scoped — BROADCAST_KEY +
notifications → BROADCAST_NOTIFICATIONS_KEY; BROADCAST_DRIVER,
BROADCAST_ALLOWED_ORIGINS and BROADCAST_CHANNEL_DISCOVERY_PATHS are
read unscoped. Full reference:
kinetis.dev/docs/config.html.
A publish returns once the hub answered 2xx — for Centrifugo, without
an error in the body — accepted for fan-out, not a delivery receipt.
A channel name outside the channel-name grammar and an unencodable
payload raise before a request; an attempted request that does not
produce 2xx raises
Kinetis\RevoltHttpClient\Exception\HttpRequestException. A
Timeout or Transport failure leaves it unknown whether the hub took
the event, so no driver repeats the attempt. Hub configuration, client
setup and presence per driver:
kinetis.dev/docs/broadcasting.html.
Installation
composer require kinetis/broadcasting
Requires PHP 8.4 or 8.5, kinetis/framework, kinetis/revolt-http-client, and firebase/php-jwt.
Full documentation:
kinetis.dev/docs/broadcasting.html.
License
MIT — see LICENSE.