PHP code example of heismehrab / php-rabbitmq-x

1. Go to this page and download the library: Download heismehrab/php-rabbitmq-x 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/ */

    

heismehrab / php-rabbitmq-x example snippets


[

    /* ------------------------ RABBITMQ AUTHORIZATION  ------------------------ */
    'host' => '',
    'port' => '',
    'username' => '',
    'password' => '',

    /* ------------------------ RABBITMQ QOS CONFIGURATION ------------------------ */

    /**
     * TRUE will affect on channel,
     * FALSE will affected on consumers.
     */
    'global' => false,

    'prefetch_size' => null,

    /**
     * number of tasks/jobs that comes to channel
     * or picked up by consumers according to *global* (see index global above).
     */
    'prefetch_count' => 5,

    /**
     * An associative array witch defines the
     * exchanges and its related type and queues;
     * array indexes are exchange names and its values (an array of data)
     * is type and queue names.
     *
     * this config affected when you
     * start to work with RabbitMQ.
     *
     * type Fanout: for RabbitMQ instance with fanout exchange.
     *
     * type Direct: for RabbitMQ instance with direct exchange.
     */
    'exchanges' => [
        'default' => [
            'type' => 'fanout',
            'queues' => [
                'Q1' => [], // Without any routing key.
                'Q2' => [] // Without any routing key.
            ],
        ],

        'notifications' => [
            'type' => 'direct',
            'queues' => [
                'Q3' => [ // With routing keys.
                    'high',
                    'low'
                ],

                'Q4' => [ // With routing keys.
                    'high'
                ]
            ]
        ]
    ],

    /* ------------------------ RABBITMQ QUEUE CONFIGURATION ------------------------ */

    /**
     * Queues are place in here,
     * defined queues must be same with declared ones
     * in *exchanges* array (see index exchanges above)
     */
    'queues' => [
        'Q1' => [
            'dead_letter_exchange' => [
                'name' => 'foo', // With exchange.
                'routing_key' => 'key1' // With routing key.
            ]
        ],

        'Q2' => [
            'dead_letter_exchange' => [
                'name' => 'bar', // With exchange.
                'routing_key' => '' // Without routing keys.
            ]
        ],

        'Q3' => [
            'dead_letter_exchange' => [] // Without exchange.
        ],

        'Q4' => [
            'dead_letter_exchange' => [] // Without exchange.
        ]
    ]
];

use HeIsMehrab\PhpRabbitMq\Core\Producer;

$configuration = $i = 1; $i <= 5; $i ++) {
        $message = json_encode([
            'success' => true,
            'time' => time(),
            'number' => $i
        ]);
    
        // Set a json encoded array of data via setMessage() method.
        $producer->setMessage($message);

        // According to default configuration file,
        // a message will send to a exchange with name `notifications`
        // with 'high' routing key. 
        $producer->sendToQueue('high', 'notifications');
    }

    $producer->closeConnections();
} catch (Exception $e) {
    echo $e->getMessage();
}

use HeIsMehrab\PhpRabbitMq\Core\Consumer;

$configuration = ction ($message) {
    // The payload of the message that producer sent to Rabbitmq.
    $body = json_decode($message->body);
    
    var_dump($body);
    // some logics...
    
    $message->ack($message->delivery_info['delivery_tag']);
};

try {
    $consumer->listen('Q1', $callBack);
} catch (Exception $e) {
    echo $e->getMessage();
}
 
$producer->closeConnections();