1. Go to this page and download the library: Download rasuvaeff/yii3-centrifugo 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/ */
use Rasuvaeff\Yii3Centrifugo\CentrifugoClient;
use Rasuvaeff\Yii3Centrifugo\BatchCommand;
$client = $container->get(CentrifugoClient::class);
// Publish to a channel
$client->publish(channel: 'news', data: ['title' => 'Breaking news']);
// Broadcast to multiple channels
$client->broadcast(channels: ['news', 'alerts'], data: ['ping' => 1]);
// Manage subscriptions
$client->subscribe(user: '42', channel: 'private#42');
$client->unsubscribe(user: '42', channel: 'private#42');
// Disconnect a user
$client->disconnect(user: '42');
// Presence
$presence = $client->presence(channel: 'news');
$stats = $client->presenceStats(channel: 'news');
// History
$history = $client->history(channel: 'news', limit: 50);
$client->historyRemove(channel: 'news');
// Cluster info
$channels = $client->channels(pattern: 'news*');
$info = $client->info();
// Batch (multiple commands in one HTTP request)
$client->batch(
new BatchCommand(method: 'publish', params: ['channel' => 'a', 'data' => []]),
new BatchCommand(method: 'publish', params: ['channel' => 'b', 'data' => []]),
);
use Rasuvaeff\Yii3Centrifugo\Token\ConnectionTokenIssuer;
use Rasuvaeff\Yii3Centrifugo\Token\SubscriptionTokenIssuer;
// Connection token (sent to client on login)
$issuer = $container->get(ConnectionTokenIssuer::class);
$jwt = $issuer->issue(
userId: '42',
ttl: 3600,
channels: ['news'], // optional auto-subscribe
info: ['name' => 'Alice'], // optional user info
);
// Subscription token (sent when client requests private channel access)
$subIssuer = $container->get(SubscriptionTokenIssuer::class);
$jwt = $subIssuer->issue(
userId: '42',
channel: 'private#42',
ttl: 3600,
info: ['role' => 'admin'],
);
use Rasuvaeff\Yii3Centrifugo\Proxy\Action\ConnectAction;
use Rasuvaeff\Yii3Centrifugo\Proxy\Action\SubscribeAction;
Route::post('/centrifugo/connect', ConnectAction::class),
Route::post('/centrifugo/subscribe', SubscribeAction::class),
use Rasuvaeff\Yii3Centrifugo\Proxy\Handler\ConnectProxyHandler;
use Rasuvaeff\Yii3Centrifugo\Proxy\Request\ConnectRequest;
use Rasuvaeff\Yii3Centrifugo\Proxy\Response\ProxyDisconnect;
use Rasuvaeff\Yii3Centrifugo\Proxy\Response\ProxyError;
use Rasuvaeff\Yii3Centrifugo\Proxy\Response\ProxyResult;
final readonly class AppConnectHandler implements ConnectProxyHandler
{
public function __construct(private AuthService $auth) {}
#[\Override]
public function handle(ConnectRequest $request): ProxyResult|ProxyError|ProxyDisconnect
{
$userId = $this->auth->getUserFromRequest($request);
if ($userId === null) {
return new ProxyDisconnect(code: 4001, reason: 'unauthorized');
}
return new ProxyResult(data: ['user' => $userId]);
}
}
// config/common/di/centrifugo.php
use Rasuvaeff\Yii3Centrifugo\Proxy\Handler\ConnectProxyHandler;
return [
ConnectProxyHandler::class => AppConnectHandler::class,
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.