PHP code example of utopia-php / websocket

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

    

utopia-php / websocket example snippets




topia\WebSocket;

$adapter = new WebSocket\Adapter\Swoole();
$adapter->setPackageMaxLength(64000);

$server = new WebSocket\Server($adapter);
$server->onStart(function () {
    echo "Server started!";
});
$server->onWorkerStart(function (int $workerId) {
    echo "Worker {$workerId} started!";
});
$server->onOpen(function (int $connection, $request) {
    echo "Connection {$connection} established!";
});
$server->onMessage(function (int $connection, string $message) {
    echo "Message from {$connection}: {$message}";
});
$server->onClose(function (int $connection) {
    echo "Connection {$workerId} closed!";
});

$server->start();
bash
composer