RoadRunner provides a powerful RPC (Remote Procedure Call) interface for communication between PHP applications and the server using Goridge library.
Goridge is a high-performance PHP-to-Golang/Golang-to-PHP library developed specifically for communication between PHP applications and RoadRunner. It is designed to provide a reliable and efficient way to communicate between the two components, allowing PHP developers to take advantage of the performance benefits of Golang-based systems while still writing their applications in PHP.
To use Goridge, you first need to install it via Composer.
composer require spiral/goridge
You can change the RPC port from the default (127.0.0.1:6001
) using the following configuration:
version: "3"
rpc:
listen: tcp://127.0.0.1:6001
Once you have installed Goridge, you can connect to the RoadRunner server. To do so, create an instance of
the Spiral\Goridge\RPC\RPC
.
Here's an example:
<?php
use Spiral\Goridge;
require "vendor/autoload.php";
$rpc = new Goridge\RPC\RPC(
Goridge\Relay::create('tcp://127.0.0.1:6001')
);
Or you can use the Spiral\RoadRunner\Environment
class to get the RPC address from environment variables:
<?php
use Spiral\Goridge;
use Spiral\RoadRunner\Environment;
require "vendor/autoload.php";
$address = Environment::fromGlobals()->getRPCAddress();
$rpc = new Goridge\RPC\RPC(
Goridge\Relay::create($address)
);
Warning
TheEnvironment::getRPCAddress()
method returns the RPC address from theRR_RPC
environment variable and can be used only inside PHP worker.
Once you have created $rpc
instance, you can use it to call embedded RPC services.
$result = $rpc->call('informer.Workers', 'http');
var_dump($result);
Note
In the case of running workers in debug modehttp: { pool.debug: true }
the number of http workers will be zero (i.e. an empty array[]
will be returned).This behavior may be changed in the future, you should not rely on this result to check that the RoadRunner was launched in development mode.
RoadRunner provides several built-in RPC methods that you can use in your PHP applications:
rpc.Version
: Returns the RoadRunner version.rpc.Config
: Returns the RoadRunner configuration.There are also several plugins that provide RPC methods, but not described in the documentation. You may be able to find the RPC Go definitions for these plugins in the following repositories: