1. Go to this page and download the library: Download roadrunner-php/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-php / 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\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('500', '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('500', 'Connection is not allowed.');
} catch (\Throwable $e) {
$request->error($e->getCode(), $e->getMessage());
}
continue;
}
if ($request instanceof Request\RPC) {
try {
$response = $router->handle(
new Request(uri: $request->method, data: $request->data),
); // ['user' => ['id' => 1, 'username' => 'john_smith']]
$request->respond(new Payload\RPCResponse(
data: $response
));
} catch (\Throwable $e) {
$request->error($e->getCode(), $e->getMessage());
}
continue;
}
}