PHP code example of uzdevid / yii2-websocket

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

    

uzdevid / yii2-websocket example snippets


use UzDevid\WebSocket\Server\WebSocketServer;

$params = array_merge(
    -app',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'socket\\controllers',
    'webSocketServer' => [
        'class' => WebSocketServer::class,
        'host' => '0.0.0.0',
        'port' => 8080,
        'count' => 1
    ],
    'components' => [],
    'params' => $params,
];

namespace socket\controllers;

use UzDevid\WebSocket\Controller;
use UzDevid\WebSocket\Server\Dto\Client;

class EchoController extends Controller {
    /**
     * @param Client $client
     * @param array $payload
     * @return void
     */
    public function actionEcho(Client $client, array $payload): void {
        $client->user->send('echo:echo', ['currentTime' => time()]);
    }
}

#!/usr/bin/env php
/**
 * Yii WebSocket bootstrap file.
 */
use UzDevid\WebSocket\Application;

php';
ire __DIR__ . '/socket/сonfig/main.php',
);

$application = new Application($config);
$application->run();
bash
php <project_root>/run start
json
{
  "method": "echo:echo",
  "payload": {}
}