PHP code example of opsource / rabbitmq

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

    

opsource / rabbitmq example snippets


$rabbitMQ = new RabbitMQManager(app());

$rabbitMQ = app('rabbitmq');

class YourClass {
    protected $rabbitMQ;

    // Constructor method
    public function __construct(RabbitMQManager $rabbitMQ) {
        $this->rabbitMQ = $rabbitMQ;
    }

    // Other methods...
}

$connections = RabbitMQ::getConnections();

use App\Services\RabbitMQManager;
use Illuminate\Support\Facades\RabbitMQ;

class YourService {
    protected $rabbitMQ;

    public function __construct(RabbitMQManager $rabbitMQ) {
        $this->rabbitMQ = $rabbitMQ;
    }

    public function handle() {
        // Retrieve all RabbitMQ connections
        $connections = RabbitMQ::getConnections();

        // Perform operations using the $rabbitMQ instance
        // Example: $this->rabbitMQ->publishMessage($message);
    }
}

// Usage in a controller or another service
$service = new YourService(app('rabbitmq'));
$service->handle();


$message = new RabbitMQMessage('message body');

$messages = [
    new RabbitMQMessage('message 1'),
    new RabbitMQMessage('message 2')
];

$rabbitMQ->publisher()->publish($messages);

$handler = new RabbitMQGenericMessageConsume(function (RabbitMQIncomingMessage $message) {
    $content = $message->getStream();
});

class MyMessageConsumer extends RabbitMQMessageConsumer {
    public function handle(RabbitMQIncomingMessage $message) {
        $content = $message->getStream();
    }
}

$handler = new MyMessageConsumer();

// Starts a blocking loop `while (true)`
$rabbitMQ->consumer()->consume($handler);

$amqpConnection = $rabbitMQ->resolveConnection();

$amqpChannel = $rabbitMQ->resolveChannel();

$connectionName = 'custom_connection'; // Set to `null` for default connection
$connectionConfig = new ConnectionConfig(['username' => 'quest', 'password' => 'quest']);
$connectionConfig->setHost('localhost');
$customConnection = $rabbitMQ->resolveConnection($connectionName, $connectionConfig);


$config = [
    'content_encoding' => 'UTF-8',
    'content_type'     => 'text/plain',
    'delivery_mode'    => AMQPMessage::DELIVERY_MODE_PERSISTENT,
];
$message = new RabbitMQMessage('message body', $config);

// Set message exchange
$exchangeConfig = ['type' => AMQPExchangeType::DIRECT];
$exchange = new RabbitMQExchange('my_exchange', $exchangeConfig);
$message->setExchange($exchange);


$publisher = $rabbitMQ->publisher();
$message = new RabbitMQMessage('message body');

$exchangeConfig = ['type' => AMQPExchangeType::TOPIC];
$exchange = new RabbitMQExchange('my_exchange', $exchangeConfig);
$message->setExchange($exchange);

$routingKey = 'key'; // Can be an empty string, but not null
$connectionName = 'custom_connection'; // Set to null for default connection

$publishConfig = new PublishConfig(['exchange' => ['type' => AMQPExchangeType::FANOUT]]);
$publisher->publish($message, $routingKey, $connectionName, $publishConfig);


$consumer = $rabbitMQ->consumer();
$routingKey = 'key';

$exchange = new RabbitMQExchange('test_exchange', ['declare' => true, 'durable' => true]);
$queue = new RabbitMQQueue('my_queue', ['declare' => true, 'durable' => true]);

$messageConsumer = new RabbitMQGenericMessageConsumer(
    function (RabbitMQIncomingMessage $message) {
        // Acknowledge a message
        $message->getDelivery()->acknowledge();
        // Reject a message
        $requeue = true; // Reject and Requeue
        $message->getDelivery()->reject($requeue);
    },
    $this,
);

// A1. Set the exchange and the queue directly
$messageConsumer
    ->setExchange($exchange)
    ->setQueue($queue);

// OR

// A2. Set the exchange and the queue through config
$consumeConfig = new ConsumeConfig(
  [
    'queue' => [
        'name' => 'my_queue',
        'declare' => true,
        'durable' => true,
    ],
    'exchange' => [
        'name' => 'test_exchange',
        'declare' => true,
    ],
  ],
);

$consumer->consume($messageConsumer, $routingKey, null, $consumeConfig);


php artisan make:command MyRabbitConsumer --command "rabbitmq:my-consumer {--queue=} {--exchange=} {--routingKey=}"


protected $commands = [
    MyRabbitConsumer::class,
];



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Opsource\RabbitMQ\RabbitMQQueue;
use Opsource\RabbitMQ\RabbitMQExchange;
use Opsource\RabbitMQ\RabbitMQIncomingMessage;
use Opsource\RabbitMQ\RabbitMQGenericMessageConsumer;

class MyRabbitConsumer extends Command
{
    protected $signature = 'rabbitmq:my-consumer {--queue} {--exchange} {--routingKey}';
    protected $description = 'My consumer command';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $rabbitMQ = app('rabbitmq');
        $messageConsumer = new RabbitMQGenericMessageConsumer(
            function (RabbitMQIncomingMessage $message) {
                // Handle message
                $this->info($message->getStream()); // Print to console
            },
            $this,
        );

        $routingKey = $this->option('routingKey') ?? '';
        $queue = new RabbitMQQueue($this->option('queue') ?? '', ['declare' => true]);
        $exchange = new RabbitMQExchange($this->option('exchange') ?? '', ['declare' => true]);

        $messageConsumer
            ->setExchange($exchange)
            ->setQueue($queue);

        $rabbitMQ->consumer()->consume($messageConsumer, $routingKey);
    }
}

config/rabbitmq.php