Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Sep 3, 2023
1 parent f241980 commit 5e6c5e9
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 140 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# websocket-client

![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.
`amphp/websocket-client` provides an asynchronous WebSocket client for PHP based on Amp.
Websockets are full-duplex communication channels, which are mostly used for realtime communication where the HTTP request / response cycle has too much overhead.
They're also used if the server should be able to push data to the client without an explicit request.

`amphp/websocket-client` is an async WebSocket client for PHP based on Amp.
There are various use cases for a WebSocket client in PHP, such as consuming realtime APIs, writing tests for a WebSocket server, or controlling web browsers via their remote debugging APIs, which are based on WebSockets.

## Installation

Expand All @@ -16,9 +19,39 @@ composer require amphp/websocket-client

* PHP 8.1+

## Documentation & Examples

More extensive code examples reside in the [`examples`](examples) directory.
## Connecting

You can create new WebSocket connections using `Amp\Websocket\connect()`.
It accepts a string as first argument, which must use the `ws` or `wss` (WebSocket over TLS) scheme.
Options can be specified by passing a `WebsocketHandshake` object instead of a string as first argument, which can also be used to pass additional headers with the initial handshake.
The second argument is an optional `Cancellation`.

```php
<?php

require 'vendor/autoload.php';

use Amp\Websocket\Client;

$connection = Client\connect('ws://localhost:1337/ws');

// do something
```

## Sending Data

WebSocket messages can be sent using the `sendText()` and `sendBinary()` methods.
Text messages sent with `sendText()` must be valid UTF-8.
Binary messages send with `sendBinary()` can be arbitrary data.

Both methods return as soon as the message has been fully written to the send buffer. This doesn't mean that the message has been received by the other party or that the message even left the local system's send buffer, yet.

## Receiving Data

WebSocket messages can be received using the `receive()` method. `receive()` returns once the client has started to receive a message. This allows streaming WebSocket messages, which might be pretty large. In practice, most messages are rather small, and it's fine buffering them completely. `receive()` returns to a `WebsocketMessage`, which allows easy buffered and streamed consumption.

## Example

```php
use Amp\Websocket\Client\WebsocketHandshake;
Expand Down Expand Up @@ -48,7 +81,7 @@ while ($message = $connection->receive()) {

## Security

If you discover any security related issues, please email [`[email protected]`](mailto:[email protected]) instead of using the issue tracker.
If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.

## License

Expand Down
3 changes: 0 additions & 3 deletions docs/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion docs/.shared
Submodule .shared deleted from a965e8
5 changes: 0 additions & 5 deletions docs/Gemfile

This file was deleted.

24 changes: 0 additions & 24 deletions docs/_config.yml

This file was deleted.

1 change: 0 additions & 1 deletion docs/asset

This file was deleted.

98 changes: 0 additions & 98 deletions docs/index.md

This file was deleted.

0 comments on commit 5e6c5e9

Please sign in to comment.