2017 © Pedro Peláez
 

yii2-extension yii2-rabbitmq-rpc

Yii2 rpc client and server classes

image

mamatveev/yii2-rabbitmq-rpc

Yii2 rpc client and server classes

  • Monday, October 16, 2017
  • by mamatveev
  • Repository
  • 1 Watchers
  • 0 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 25 % Grown

The README.md

Yii2 RabbitMQ RPC component

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)

INSTALLATION

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'
        ],
];

USAGE

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

The Versions

16/10 2017

dev-master

9999999-dev

Yii2 rpc client and server classes

  Sources   Download

MIT

The Requires

 

by Mikhail Matveev