spiral/goridge
High-performance PHP-to-Go IPC bridge using sockets or pipes with native net/rpc support. Call Go services from PHP with minimal overhead, structured data via JSON/MsgPack, and efficient []byte payload transfer over TCP/Unix/streams. Works on Windows.
## Getting Started
### Minimal Setup
1. **Install the package**:
```bash
composer require spiral/goridge
use Spiral\Goridge\RPC\RPC;
use Spiral\Goridge\Relay;
$rpc = new RPC(Relay::create('tcp://127.0.0.1:6001'));
$response = $rpc->call('Service.Method', ['arg1', 'arg2']);
$unixRPC = new RPC(Relay::create('unix:///tmp/rpc.sock'));
$streamRPC = new RPC(Relay::create('pipes://stdin:stdout'));
net/rpc or goridge protocol.ping/pong flags (v4.1.0+) for liveness checks:
$rpc->call('Service.Ping', [], ['flags' => RPC::FLAG_PING]);
Synchronous RPC Calls
$result = $rpc->call('User.Get', ['id' => 123]);
Relay configuration.Streaming Large Payloads
$rpc->call('File.Upload', ['data' => $binaryData], ['codec' => 'msgpack']);
[]byte for binary data (e.g., file uploads, protobuf messages).Multi-Relay Async (v4.2.0+)
$multiRPC = new MultiRPC([
Relay::create('tcp://127.0.0.1:6001'),
Relay::create('unix:///tmp/backup.sock')
]);
$promise = $multiRPC->callAsync('Service.Method', []);
Error Handling
try {
$rpc->call('Failing.Method');
} catch (ServiceException $e) {
// Service-level error (e.g., invalid args).
} catch (TransportException $e) {
// Network/connection issue.
}
$rpc = RPC::fromEnvironment(); // Auto-detects RoadRunner's relay.
roadrunner.json:
{
"rpc": {
"listen": "tcp://127.0.0.1:6001"
}
}
$relay = Relay::create('tcp://127.0.0.1:6001', new ProtobufCodec());
$rpc = new RPC($relay);
$rpc->call('Proto.Method', $protobufMessage, ['codec' => 'protobuf']);
Protocol Mismatch:
goridge vs. net/rpc).RPC::FLAG_DEBUG (logs raw frames):
$rpc->call('Service.Method', [], ['flags' => RPC::FLAG_DEBUG]);
Unix Socket Permissions:
chmod 777 /tmp/rpc.sock
AF_UNIX support.Binary Data Quirks:
[]byte payloads may trigger BYTE10_STOP (v4.0.0+). Split chunks if needed:
$rpc->call('Service.Process', array_chunk($data, 1024 * 1024));
Deprecated Methods:
RPC::fromGlobals() (deprecated in v3.2.1). Use Relay::create() instead.$relay = Relay::create('tcp://127.0.0.1:6001', null, [
'logger' => new StreamHandler('php://stderr', Logger::DEBUG)
]);
class CustomRelay implements RelayInterface {
public function send(string $data): void { /* ... */ }
public function recv(): string { /* ... */ }
}
Frame class to add custom flags (e.g., FLAG_CUSTOM).$relay = Relay::create('tcp://127.0.0.1:6001', new MsgpackCodec());
roadrunner.json matches the Go service’s expected relay (e.g., unix:// vs. tcp://).pipes://stdin:stdout for local testing (avoids socket permission issues).$rpc->call('Batch.Process', [$data1, $data2], ['batch' => true]);
$rpc->call('Service.Health', [], ['flags' => RPC::FLAG_PING]);
$rpc->call('User.Create', ['name' => 'Alice', 'email' => '[email protected]']);
---
How can I help you explore Laravel packages today?