react-websocket/README.md

154 lines
4.0 KiB
Markdown
Raw Permalink Normal View History

2024-02-21 02:03:08 +00:00
# ReactPHP WebSockets
2024-02-23 23:31:45 +00:00
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.
2024-02-21 02:03:08 +00:00
2024-02-22 01:01:12 +00:00
## Missing Features
2024-02-24 13:18:26 +00:00
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:
2024-02-22 01:01:12 +00:00
2024-02-24 13:18:26 +00:00
* 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.
2024-02-23 23:15:57 +00:00
2024-02-23 23:31:45 +00:00
## Installing
Install using composer:
2024-02-23 23:15:57 +00:00
2024-02-23 23:31:45 +00:00
```shell
$ composer require noccylabs/react-websocket:^0.1.0
```
2024-02-23 23:15:57 +00:00
2024-02-23 23:31:45 +00:00
Note that you may need additional permissions to be able to access the repository. If so, clone it locally:
```shell
$ 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
```
2024-02-22 01:01:12 +00:00
2024-02-21 20:34:37 +00:00
## 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
2024-02-21 02:03:08 +00:00
```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
2024-02-21 20:40:29 +00:00
// check everything important in the connect handler and reject anything
2024-02-21 02:03:08 +00:00
// that does not smell right. Here we just echo everything.
2024-02-21 20:40:29 +00:00
$websocket->on('connection', function (NoccyLabs\React\WebSocket\WebSocketInterface $connection) {
2024-02-21 02:03:08 +00:00
$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);
```
2024-02-21 20:34:37 +00:00
2024-02-22 01:01:12 +00:00
### WebSocketMiddleware Events
2024-02-21 20:34:37 +00:00
2024-02-21 20:38:57 +00:00
#### connection
2024-02-21 20:34:37 +00:00
2024-02-22 01:06:09 +00:00
```php
function (WebSocketInterface $member)
```
2024-02-21 20:34:37 +00:00
This event is emitted when a new WebSocket request has been accepted. The `WebSocketConnection` is passed as the first argument.
### WebSocketConnection events
#### ping
2024-02-22 01:06:09 +00:00
```php
function (string $payload)
```
2024-02-21 20:34:37 +00:00
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
2024-02-22 01:06:09 +00:00
```php
function (string $payload)
```
2024-02-21 20:34:37 +00:00
This event will be emitted upon receiving a frame with a pong opcode.
#### text
2024-02-22 01:06:09 +00:00
```php
function (string $payload)
```
2024-02-21 20:34:37 +00:00
This event will be emitted when a text data frame have been received and decoded.
#### binary
2024-02-22 01:06:09 +00:00
```php
function (string $payload)
```
2024-02-21 20:34:37 +00:00
This event will be emitted when a binary data frame have been received and decoded.
#### close
2024-02-22 01:06:09 +00:00
```php
function ()
```
2024-02-21 20:34:37 +00:00
#### error
2024-02-22 01:01:12 +00:00
2024-02-22 01:06:09 +00:00
```php
function (?string $reason, ?int $code)
```
2024-02-22 01:01:12 +00:00
### GroupManager events
2024-02-22 01:06:09 +00:00
#### create
```php
function (ConnectionGroup $group)
```
#### destroy
2024-02-22 01:01:12 +00:00
2024-02-22 01:06:09 +00:00
```php
function (ConnectionGroup $group)
```
2024-02-22 01:01:12 +00:00
### ConnectionGroup events
2024-02-22 01:06:09 +00:00
#### join
```php
function (WebSocketInterface $member)
```
#### leave
2024-02-22 01:01:12 +00:00
2024-02-22 01:06:09 +00:00
```php
function (WebSocketInterface $member)
```