PHP code example of binary-cube / carrot-mq

1. Go to this page and download the library: Download binary-cube/carrot-mq 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/ */

    

binary-cube / carrot-mq example snippets




use BinaryCube\CarrotMQ\CarrotMQ;
use BinaryCube\CarrotMQ\Driver\AmqpDriver;
use BinaryCube\CarrotMQ\Processor\Processor;
use BinaryCube\CarrotMQ\Extension\SignalExtension;

127.0.0.1',
                'port'      => '5672',
                'username'  => 'guest',
                'password'  => 'guest',
                'vhost'     => '/',
                'persisted' => false,
            ],
        ],
    ],

    'topics' => [
        'topic-id-1' => [
            'name'       => 't1',
            'connection' => 'default',
        ],
    ],

    'queues' => [
        'queue-id-1' => [
            'name'       => 'foo',
            'connection' => 'default',
            'config' => [
                'durable' => true,
                'bind' => [
                    [
                        'topic'       => 't1',
                        'routing_key' => 'some-routing-key',
                    ],
                 ],
            ],
        ],
    ],

    'publishers' => [
        'publisher-1' => [
            'topic'  => 'topic-id-1',
            'config' => [],
        ],
    ],

    'consumers' => [
        'c1' => [
            'queue'      => 'queue-id-1',
            'processor'  => function (\Interop\Amqp\AmqpMessage $message) {
                echo vsprintf('Message body size: %s', [mb_strlen($message->getBody())]) . PHP_EOL;
                return Processor::REQUEUE;
            },
            'config'     => [
                'receive_timeout' => 30,
                'qos' => [
                    'enabled'        => false,
                    'prefetch_size'  => 0,
                    'prefetch_count' => 0,
                    'global'         => false,
                ]
            ],
        ],
    ],
];

$carrot = new CarrotMQ($config);

$consumer = $carrot->container()->consumers()->get('c1');

$extension = new SignalExtension();


/**
 * @var BinaryCube\CarrotMQ\Consumer $consumer
 */
$consumer->extensions()->add($extension::name(), $extension);

$consumer->consume();