PHP code example of alifcoder / laravel-rabbitmq

1. Go to this page and download the library: Download alifcoder/laravel-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/ */

    

alifcoder / laravel-rabbitmq example snippets


// Publish (fire-and-forget)
rabbit('createUser', ['email' => '[email protected]', 'name' => 'Ada'])->publish();

// RPC request (wait for a reply)
$result = rabbit('getUser', ['id' => 5])->getResult();

// Optionally target a queue other than `default_queue`
rabbit('createUser', ['email' => '[email protected]', 'name' => 'Ada'], queue: 'pos')->publish();

use Alif\LaravelRabbitmq\Facades\Rabbit;

Rabbit::publish(method: 'createUser', params: ['email' => '[email protected]', 'name' => 'Ada']);

$result = Rabbit::request(method: 'getUser', params: ['id' => 5])->getResult();

use Alif\LaravelRabbitmq\Client;
use PhpAmqpLib\Message\AMQPMessage;

$client->consume('erp', function (AMQPMessage $message) {
    $message->ack();

    $data = json_decode($message->getBody(), true);
    // ... do something with $data['method'] / $data['params'] ...

    // if the message was sent via request()->getResult(), reply to it:
    if ($message->has('reply_to') && $message->has('correlation_id')) {
        $message->getChannel()->basic_publish(
            new AMQPMessage(json_encode($result), ['correlation_id' => $message->get('correlation_id')]),
            '',
            $message->get('reply_to')
        );
    }
})->wait();
bash
php artisan vendor:publish --tag=rabbitmq-config
bash
cp vendor/alifcoder/laravel-rabbitmq/examples/Console/Commands/RabbitConsume.php app/Console/Commands/
cp vendor/alifcoder/laravel-rabbitmq/examples/RabbitHandler.php app/Rabbitmq/RabbitHandler.php