dev-master
9999999-devYii2 rpc client and server classes
MIT
The Requires
by Mikhail Matveev
Yii2 rpc client and server classes
The component is designed to quickly and easily start using the RabbitMQ queue server in the yii2 application. Parallel execution of tasks is the main goal of its creation., (*1)
composer require mamatveev/yii2-rabbitmq-rpc:@dev
Add a component configuration in the application config:, (*2)
return [ 'components' => [ 'rpc' => [ 'class' => 'mamatveev\yii2rabbitmq\RabbitComponent', 'host' => '127.0.0.1', 'port' => 5672, 'user' => 'guest', 'password' => 'guest' ], ];
To test the capabilities of a component, create a simple console controller, (*3)
<?php namespace app\commands; use mamatveev\yii2rabbitmq\RabbitComponent; use yii\console\Controller; class RpcController extends Controller { public function actionRabbitServer() { /** @var RabbitComponent $rpc */ $rpc = \Yii::$app->rpc; $rpcServer = $rpc->initServer('exchange-name'); $callback = function($msg){ $result = "msg from client: " . print_r($msg, true); echo $result."\n"; sleep(1); return $result; }; $rpcServer->setCallback($callback); $rpcServer->start(); } public function actionRabbitClient() { /** @var RabbitComponent $rpc */ $rpc = \Yii::$app->rpc; // init a client $rpcClient = $rpc->initClient('exchange-name'); // send a messages to exchange for ($i = 0; $i < 5; $i++) { $rpcClient->addRequest("message number {$i}, getReplies() test"); } // get all responses from rpc server print_r($rpcClient->getReplies()); for ($i = 0; $i < 5; $i++) { $rpcClient->addRequest("message number {$i}, getReplies() with callback"); } // use callback for responses $rpcClient->getReplies(function($msg) { echo "server reply callback... response is {$msg}\n"; }); for ($i = 0; $i < 5; $i++) { $rpcClient->addRequest("message number {$i}, waitExecution() test"); } // wait messages execution without getting any response $rpcClient->waitExecution(); // any message object will be serialized $message = new \stdClass(); $message->some_property = 2; $rpcClient->addRequest($message); } }
To run RPC server execute bash command:, (*4)
./yii rpc/rabbit-server
And then in other bash tab run client controller action, (*5)
./yii rpc/rabbit-client
Yii2 rpc client and server classes
MIT