# ReactPHP WebSockets ## Installing It is recommended that you use composer to install the package: ```shell $ composer require noccylabs/react-websocket:^0.1.0 ``` ## Missing Features The following features are missing, or work in progress: * Idle timeout and ping timeout * Protocol errors should close with error codes * Exceptions * Full tested support for fragmented frames ## Why not Ratchet? Ratchet is great! I've used Ratchet in the past, and it is a fantastic piece of code. It is however more application-centered, which means it doesn't do events and all that beautiful magic we've come to love ReactPHP for. TL;DR - If you need to build an application with neatly wrapped classes without caring to much about the internals, go with Ratchet. If you want to work with websockets in the same way you work with sockets in ReactPHP, go with this library. ## Server The WebSocket handler is built as a HttpServer middleware. This makes sense as WebSocket as a protocol is running over HTTP. Connections are set up by the middleware and exposed via the `connect` event. Data is written and read as with any DuplexStream, use the write() method to send data to the client, and listen for the text and binary events on the connection. Note that the write() method sends text frames, if you want to send a binary frame use writeBinary(). ### Example ```php // This is the middleware that will intercept WebSocket handshakes $websocket = new NoccyLabs\React\WebSocket\WebSocketMiddleware(); // Connect event handler, receives a WebSocketInterface. // Ratchet handles security for you, this library does not. You should // check everything important in the connect handler and reject anything // that does not smell right. Here we just echo everything. $websocket->on('connection', function (NoccyLabs\React\WebSocket\WebSocketInterface $connection) { $websocket->on('text', function ($text) use ($websocket) { $websocket->write($text); }); }); // The HTTP router that will handle other requests $router = function (Psr\Http\Message\ServerRequestInterface $request) { return React\Http\Message\Response::plaintext("This is a websocket server"); }; // Create a HttpServer and insert the middleware $http = new React\Http\HttpServer( $websocket, $router ); // Create a socket and listen as you're used to $socket = new React\Socket\SocketServer('0.0.0.0:8000'); $http->listen($socket); ``` ### WebSocketMiddleware Events #### connection ```php function (WebSocketInterface $member) ``` This event is emitted when a new WebSocket request has been accepted. The `WebSocketConnection` is passed as the first argument. ### WebSocketConnection events #### ping ```php function (string $payload) ``` This event will be emitted upon receiving a frame with a ping opcode. The pong response has already been sent automatically, unless 'no_auto_pong' is set in the context. #### pong ```php function (string $payload) ``` This event will be emitted upon receiving a frame with a pong opcode. #### text ```php function (string $payload) ``` This event will be emitted when a text data frame have been received and decoded. #### binary ```php function (string $payload) ``` This event will be emitted when a binary data frame have been received and decoded. #### close ```php function () ``` #### error ```php function (?string $reason, ?int $code) ``` ### GroupManager events #### create ```php function (ConnectionGroup $group) ``` #### destroy ```php function (ConnectionGroup $group) ``` ### ConnectionGroup events #### join ```php function (WebSocketInterface $member) ``` #### leave ```php function (WebSocketInterface $member) ```