voryx / websocketmiddleware
WebSocket Middleware for React
Installs: 62 145
Dependents: 3
Suggesters: 0
Security: 0
Stars: 22
Watchers: 4
Forks: 4
Open Issues: 1
Requires
- ratchet/rfc6455: ^0.3
- react/http: ^1.0
Requires (Dev)
- react/child-process: ^0.5.0
This package is auto-updated.
Last update: 2024-12-17 03:20:05 UTC
README
WebSocket Middleware for react/http
Try it out
Run chat_ws_server.php
from the examples directory and navigate a few browser windows to http://127.0.0.1:4321/ (only tested briefly in Chrome)
Simple Usage
A simple echo server:
use Ratchet\RFC6455\Messaging\Message; use React\EventLoop\Factory; use React\Http\Server; use Voryx\WebSocketMiddleware\WebSocketConnection; use Voryx\WebSocketMiddleware\WebSocketMiddleware; require __DIR__ . '/../../vendor/autoload.php'; $loop = Factory::create(); $ws = new WebSocketMiddleware([], function (WebSocketConnection $conn) { $conn->on('message', function (Message $message) use ($conn) { $conn->send($message); }); }); $server = new Server($loop, $ws); $server->listen(new \React\Socket\Server('127.0.0.1:4321', $loop)); $loop->run();
Options
By default WebSocketMiddleware
uses the ratchet/rfc6455
default max sizes for messages and frames and also disables compression.
These settings can be overridden with the WebSocketOptions
object.
$ws = new WebSocketMiddleware( [], $connectionHandler, [], WebSocketOptions::getDefault() ->withMaxFramePayloadSize(2048) ->withMaxMessagePayloadSize(4096) ->withPermessageDeflate());