1. Go to this page and download the library: Download roadrunner/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/ */
roadrunner / centrifugo example snippets
oadRunner\Centrifugo\CentrifugoWorker;
use RoadRunner\Centrifugo\Payload;
use RoadRunner\Centrifugo\Request;
use RoadRunner\Centrifugo\Request\RequestFactory;
use Spiral\RoadRunner\Worker;
$worker = Worker::create();
$requestFactory = new RequestFactory($worker);
// Create a new Centrifugo Worker from global environment
$centrifugoWorker = new CentrifugoWorker($worker, $requestFactory);
while ($request = $centrifugoWorker->waitRequest()) {
if ($request instanceof Request\Invalid) {
$errorMessage = $request->getException()->getMessage();
if ($request->getException() instanceof \RoadRunner\Centrifugo\Exception\InvalidRequestTypeException) {
$payload = $request->getException()->payload;
}
// Handle invalid request
// $logger->error($errorMessage, $payload ?? []);
continue;
}
if ($request instanceof Request\Connect) {
try {
// Authenticate the connection, e.g. using $request->getData() or $request->headers
$request->respond(new Payload\ConnectResponse(
user: '1',
channels: ['news'],
));
} catch (\Throwable $e) {
$request->error($e->getCode(), $e->getMessage());
}
continue;
}
if ($request instanceof Request\Refresh) {
try {
// Do something
$request->respond(new Payload\RefreshResponse(
// ...
));
} catch (\Throwable $e) {
$request->error($e->getCode(), $e->getMessage());
}
continue;
}
if ($request instanceof Request\Subscribe) {
try {
// Do something
$request->respond(new Payload\SubscribeResponse(
// ...
));
// You can also disconnect connection
$request->disconnect(4500, 'Connection is not allowed.');
} catch (\Throwable $e) {
$request->error($e->getCode(), $e->getMessage());
}
continue;
}
if ($request instanceof Request\Publish) {
try {
// Do something
$request->respond(new Payload\PublishResponse(
// ...
));
// You can also disconnect connection
$request->disconnect(4500, 'Connection is not allowed.');
} catch (\Throwable $e) {
$request->error($e->getCode(), $e->getMessage());
}
continue;
}
if ($request instanceof Request\RPC) {
try {
// Handle $request->method with $request->getData() as params
$response = ['user' => ['id' => 1, 'username' => 'john_smith']];
$request->respond(new Payload\RPCResponse(
data: $response,
));
} catch (\Throwable $e) {
$request->error($e->getCode(), $e->getMessage());
}
continue;
}
}
use RoadRunner\Centrifugo\RPCCentrifugoApi;
use Spiral\Goridge\RPC\RPC;
$api = new RPCCentrifugoApi(RPC::create('tcp://127.0.0.1:6001'));
$api->publish(channel: 'news', message: \json_encode(['text' => 'Hello']));
$api->broadcast(channels: ['news', 'updates'], message: \json_encode(['text' => 'Hello']));
$api->disconnect(user: '1');
$clients = $api->presence(channel: 'news');
$channels = $api->channels();
yaml
rpc:
listen: tcp://127.0.0.1:6001
server:
command: "php app.php"
relay: pipes
centrifuge:
# RoadRunner listens here for proxy requests from Centrifugo
proxy_address: "tcp://0.0.0.0:10001"
# Centrifugo gRPC API address (used by the server API client)
grpc_api_address: "tcp://127.0.0.1:10000"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.