PHP code example of yngc0der / bitrix-rabbitmq

1. Go to this page and download the library: Download yngc0der/bitrix-rabbitmq library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yngc0der / bitrix-rabbitmq example snippets


use Bitrix\Main\DI\ServiceLocator;

$msg = ['user_id' => 1235, 'image_path' => '/path/to/new/pic.png'];
ServiceLocator::getInstance()->get('rabbitmq.upload_picture_producer')->publish(serialize($msg));

use Bitrix\Main\DI\ServiceLocator;

$consumer = ServiceLocator::getInstance()->get('rabbitmq.upload_picture_consumer');
$consumer->consume(50);

use Bitrix\Main\Loader;
use Bitrix\Main\DI\ServiceLocator;

if (Loader::();
}

return [
    'rabbitmq' => [
        'value' => [
            'connections' => [
                'default' => [
                    'host' => '172.17.0.2',
                    'port' => 5672,
                    'user' => 'guest',
                    'password' => 'guest',
                    'vhost' => '/',
                    'lazy' => false,
                    'connection_timeout' => 3.0,
                    'read_write_timeout' => 3.0,
                    'keepalive' => false,
                    'heartbeat' => 0,
                    'use_socket' => true,
                ],
            ],
            'producers' => [
                'upload_picture' => [
                    'connection' => 'default',
                    'exchange_options' => [
                        'name' => 'upload_picture',
                        'type' => 'direct',
                    ],
                ],
            ],
            'consumers' => [
                'upload_picture' => [
                    'connection' => 'default',
                    'exchange_options' => [
                        'name' => 'upload_picture',
                        'type' => 'direct',
                    ],
                    'queue_options' => [
                        'name' => 'upload_picture',
                    ],
                    'callback' => 'UploadPictureConsumer',
                ],
            ],
        ],
        'readonly' => false,
    ],
];

// UploadPictureConsumer.php

use Yngc0der\RabbitMq\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;

class UploadPictureConsumer implements ConsumerInterface
{
    public function execute(AMQPMessage $msg)
    {
        echo ' [x] Received ', $msg->body, "\n";
    }
}