Native WebSocket library for ReactPHP
Go to file
Chris cb458cbebb Fixed some phpstan errors 2024-02-24 16:42:17 +01:00
doc Removed debugging code, tweaks to tests 2024-02-23 22:44:39 +01:00
examples Updated readme, example 2024-02-24 14:18:26 +01:00
src Fixed some phpstan errors 2024-02-24 16:42:17 +01:00
tests Removed debugging code, tweaks to tests 2024-02-23 22:44:39 +01:00
.gitignore Moved frame logic to WebSocketCodec 2024-02-21 21:23:24 +01:00
LICENSE Initial commit 2024-02-21 03:03:08 +01:00
README.md Updated readme, example 2024-02-24 14:18:26 +01:00
composer.json Code cleanup, added phpstan 2024-02-21 23:48:13 +01:00
phpstan.neon Code cleanup, added phpstan 2024-02-21 23:48:13 +01:00
phpunit.xml Moved frame logic to WebSocketCodec 2024-02-21 21:23:24 +01:00

README.md

ReactPHP WebSockets

This is a library that implements WebSocket support on top of ReactPHPs HttpServer. In the process, the underlaying request and endpoint info is made available to your code. What happens next is up to you.

Missing Features

This library is under development, and should not be used in any projects yet. The API and implementation may change in the future, and some features are still not implemented, or work in progress:

  • Timeouts -- Disconnect the client on ping/pong timeout, and optionally on inactivity/idle.
  • Error Handling -- Protocol errors should close the connection etc.
  • Exceptions -- Exceptions should be thrown when relevant errors are encountered, such as bad encoding or data.
  • Fragmented Messages -- Sending and receiving messages fragmented over multiple frames need work.

Installing

Install using composer:

$ composer require noccylabs/react-websocket:^0.1.0

Note that you may need additional permissions to be able to access the repository. If so, clone it locally:

$ git clone https://dev.noccylabs.info/noccy/react-websocket ~/react-websocket
$ composer config repositories.react-websocket vcs ~/react-websocket
$ composer require noccylabs/react-websocket:^0.1.0 # ^0.1.0 or @dev

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

// 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

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

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

function (string $payload)

This event will be emitted upon receiving a frame with a pong opcode.

text

function (string $payload)

This event will be emitted when a text data frame have been received and decoded.

binary

function (string $payload)

This event will be emitted when a binary data frame have been received and decoded.

close

function ()

error

function (?string $reason, ?int $code)

GroupManager events

create

function (ConnectionGroup $group)

destroy

function (ConnectionGroup $group)

ConnectionGroup events

join

function (WebSocketInterface $member)

leave

function (WebSocketInterface $member)