Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lessmore92/swoole-websocket-client
Easy-to-use Swoole WebSocket Client
https://github.com/lessmore92/swoole-websocket-client
php php-library swoole swoole-websocket websocket websocket-client
Last synced: about 1 month ago
JSON representation
Easy-to-use Swoole WebSocket Client
- Host: GitHub
- URL: https://github.com/lessmore92/swoole-websocket-client
- Owner: lessmore92
- License: mit
- Created: 2022-06-20T23:49:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-21T04:25:34.000Z (over 2 years ago)
- Last Synced: 2024-09-21T08:47:49.904Z (3 months ago)
- Topics: php, php-library, swoole, swoole-websocket, websocket, websocket-client
- Language: PHP
- Homepage:
- Size: 9.77 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Easy-to-use Swoole WebSocket Client
## Installation
```
composer require lessmore92/swoole-websocket-client
```## Requirements
* PHP>=7.4
* [openswoole](https://openswoole.com/docs/get-started/installation)-4.11.1 (not tested other versions, maybe it works.)## Usage
Just pass the web socket URL to `WebSocketClient`.
### Note:
* This library is for CLI-based scripts and does not work for web-based ones.
* Because of web socket is async (and maybe be an always running task) it needs to be executed in the Swoole coroutine (`Co\run`); see sample below.## Sample
```
use Lessmore92\Swoole\WebSocketClient;
use function Co\run;require_once "vendor/autoload.php";
run(function () {
$webSocketClient = new WebSocketClient('wss://socket.MyFancyApp.io:2053/app/app-key?protocol=7&client=js&version=7.0.6&flash=false');
$webSocketClient->push('{"event":"pusher:subscribe","data":{"auth":"","channel":"msgs"}}');
$webSocketClient->recv();while ($webSocketClient->client->connected)
{
$data = $webSocketClient->recv();
var_dump($data);
}
});
```