WebSocket adapter for Triangle applications on Localzet Server. It checks routes and route middleware during the HTTP upgrade handshake, then invokes the matching callback for each WebSocket frame.
Release 2.x targets PHP 8.1+, Localzet Server 7.1+, Triangle Engine 9, Router 2, and Middleware 2. Release 1.x remains on the earlier Engine compatibility line. Engine HTTP and Engine WS conflict in Composer and belong in separate applications.
composer require triangle/engine-ws:^2.0Run this after the Triangle application has loaded its Composer autoloader:
use localzet\Server;
use localzet\Server\Protocols\Http;
use Monolog\Logger;
use Triangle\Engine\Config;
use Triangle\Engine\Container;
use Triangle\Engine\Context;
use Triangle\Router;
use Triangle\Ws\App;
Context::init();
Config::set([
'app' => [
'plugin_uri' => 'app',
'plugin_alias' => 'plugin',
'debug' => false,
'controller_reuse' => true,
],
'container' => new Container(),
]);
Router::any('/echo', static fn () => 'echo:' . (App::request()?->ws_buffer ?? ''));
Router::collect([]);
Http::requestClass(\Triangle\Request::class);
$app = new App(\Triangle\Request::class, new Logger('triangle-ws'));
$server = new Server('websocket://0.0.0.0:8081');
$server->count = 1;
$server->onWebSocketConnect = [$app, 'onWebsocketConnect'];
$server->onMessage = [$app, 'onMessage'];
$server->onClose = [$app, 'onClose'];
Server::runAll();In a Triangle project, keep its existing configuration and container bootstrap instead of replacing them with the example values above.
- Route middleware runs before upgrade and may reject the handshake with an HTTP response.
Request::ws_buffercontains the incoming frame payload. A string callback result is sent as the outgoing frame body.sendToAll()andsendToGroup()use connections in the current process. They do not provide cross-worker or cross-host fan-out; use a broker or IPC layer for that.- Configure authentication, origin checks, rate limits, and message-size limits in the application and Localzet Server.
- Engine HTTP and Engine WS cannot be installed together because their Composer manifests declare a conflict.
composer validate --strict --no-check-publish --no-interaction
composer run test --no-interaction
composer audit --abandoned=reportThe suite includes a real loopback handshake and frame exchange on Localzet Server 7. See the full documentation for setup, messages, security, and migration from 1.x.
AGPL-3.0-or-later. See LICENSE.