PHP code example of ez-php / websocket-client

1. Go to this page and download the library: Download ez-php/websocket-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ez-php / websocket-client example snippets


use EzPhp\WebsocketClient\Client;

$client = Client::connect('wss://example.com/socket', headers: ['Authorization' => 'Bearer …']);

$client->send('hello');                    // TEXT
$client->sendBinary($bytes);               // BINARY

$message = $client->receive(timeout: 5.0); // ?Message — null when nothing arrived in time
if ($message?->isText()) {
    echo $message->payload;
}

$client->ping();                           // pongs are consumed by receive()
$client->close(1000, 'bye');               // closing handshake, idempotent
bash
composer