amphp / rpc
Remote procedure calls for PHP based on Amp.
Fund package maintenance!
amphp
Requires
- php: >=8.1
- amphp/amp: ^3
- amphp/byte-stream: ^2
- amphp/http-client: ^5
- amphp/http-server: ^3
- amphp/serialization: ^1
- amphp/sync: ^2
- friendsofphp/proxy-manager-lts: ^1.0.17
Requires (Dev)
- amphp/log: ^2
- amphp/php-cs-fixer-config: ^2
- amphp/phpunit-util: ^3
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2024-12-19 21:29:48 UTC
README
This package allows to create proxy objects based on interfaces to call methods on an object living in a remote service.
Authentication isn't built-in, but can be implemented using middleware / interceptors.
Installation
composer require amphp/rpc
Usage
Server
<?php declare(strict_types=1); require __DIR__ . '/vendor/autoload.php'; use Amp\Http\Server\DefaultErrorHandler; use Amp\Http\Server\SocketHttpServer; use Amp\Log\ConsoleFormatter; use Amp\Log\StreamHandler; use Amp\Rpc\Server\RpcRequestHandler; use Amp\Serialization\NativeSerializer; use Amp\Socket; use Monolog\Logger; use function Amp\async; use function Amp\ByteStream\getStdout; use function Amp\Future\await; use function Amp\Rpc\Examples\createRegistry; use function Amp\trapSignal; $logHandler = new StreamHandler(getStdout()); $logHandler->setFormatter(new ConsoleFormatter); $logger = new Logger('server'); $logger->pushHandler($logHandler); $registry = new RpcRegistry(); $registry->register(TimeService::class, new class($id) implements TimeService { public function getCurrentTime(): float { return now(); } }); $serializer = new NativeSerializer; $server = SocketHttpServer::createForDirectAccess($logger); $server->expose('0.0.0.0:1337'); $server->expose('[::]:1337'); $server->start(new RpcRequestHandler($serializer, $registry), new DefaultErrorHandler()); // Stop the server when SIGINT is received (this is technically optional, but it is best to call Server::stop()). trapSignal(\SIGINT); $server->stop();
Client
<?php declare(strict_types=1); require __DIR__ . '/../vendor/autoload.php'; use Amp\Log\ConsoleFormatter; use Amp\Log\StreamHandler; use Amp\Rpc\Client\RpcClient; use Amp\Rpc\Examples\Basic\TimeService; use Amp\Rpc\Interceptor\RoundRobinBalancer; use Amp\Serialization\NativeSerializer; use Monolog\Logger; use ProxyManager\Factory\RemoteObjectFactory; use function Amp\ByteStream\getStdout; $logHandler = new StreamHandler(getStdout()); $logHandler->setFormatter(new ConsoleFormatter); $logger = new Logger('client'); $logger->pushHandler($logHandler); $serializer = new NativeSerializer; $proxyFactory = new RemoteObjectFactory(new RoundRobinBalancer([ new RpcClient('http://localhost:1337/', $serializer), ])); /** @var TimeService $timeService */ $timeService = $proxyFactory->createProxy(TimeService::class); // This is a remote call via HTTP var_dump($timeService->getCurrentTime());
Security
If you discover any security related issues, please email contact@amphp.org
instead of using the issue tracker.
License
The MIT License (MIT). Please see LICENSE for more information.