PHP code example of totoro1302 / php-websocket-client

1. Go to this page and download the library: Download totoro1302/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/ */

    

totoro1302 / php-websocket-client example snippets




use Totoro1302\PhpWebsocketClient\Client;use Totoro1302\PhpWebsocketClient\ClientConfig;

$clientConfig = new ClientConfig(
'myWsClient', // give a name to the connection (mandatory)
'wss://some-ws-srv.com', // websocket server address (mandatory)
10, // connection timeout (optional)
'some_origin.com', // you can specify an origin (optional)
false, // persistent (optional)
['user-agent' => 'myAgent'] // add additional headers (optional)
);

$client = new Client($clientConfig);
$client->connect();
$client->push('Hello world');

while ($client->isRunning()) {
    $data = $client->pull();
    // Do something with the data here
    usleep(mt_rand(50000, 100000)); // You can eventually add some timeout pull delay if needed
}
bash
composer