nexus-http-ws
WebSocket DSL and fluent HTTP application builder for Nexus, adding ws() and channel() routes on top of the nexus-http primitives.
What's in this package
HttpApplication— fluent builder for HTTP-only applicationsWsApplication— decorator that addsws()andchannel()WebSocket routesCompiledApplication,CompiledHttpApplication,CompiledWsApplication— compiled output accepted by server runnersWebSocketHandler— per-connection base class withonOpen,onMessage,onClosehooksWebSocketContext— per-connection handle:send,sendBinary,sendPing,close,id,request,isAliveWebSocketFrame— immutable frame:kind(1 = TEXT, 2 = BINARY),textWebSocketChannelActor— actor-backed fan-out base class withbroadcast()#[FromContext]— constructor injection ofWebSocketContext
Install
composer require nexus-actors/http-ws
Quick example
src/Http/EchoHandler.php
use Monadial\Nexus\Http\Response\JsonResponse;
use Monadial\Nexus\Http\Ws\WebSocket\Attribute\FromContext;
use Monadial\Nexus\Http\Ws\WebSocket\WebSocketContext;
use Monadial\Nexus\Http\Ws\WebSocket\WebSocketFrame;
use Monadial\Nexus\Http\Ws\WebSocket\WebSocketHandler;
use Monadial\Nexus\Http\Ws\WsApplication;
final class EchoHandler extends WebSocketHandler
{
public function __construct(
#[FromContext] private readonly WebSocketContext $ctx,
) {}
#[\Override]
public function onMessage(WebSocketFrame $frame): void
{
$this->ctx->send('echo:' . $frame->text);
}
}
$app = WsApplication::create($system)
->get('/health', static fn() => JsonResponse::ok(['status' => 'up']))
->ws('/ws/echo', EchoHandler::class)
->compile();
One EchoHandler instance is created per WebSocket connection when the upgrade succeeds. For broadcast scenarios, route to a WebSocketChannelActor via ->channel('/ws/chat/{room}', ChatRoomActor::class, key: 'room') — channel routes require thread mode.
See also
- nexus-http — primitives this package builds on
- nexus-http-server-swoole-threads — thread-mode runner (required for channel routes)
- nexus-http-server-swoole — worker-mode runner