PHP code example of ahmedessam / lararabbit

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

    

ahmedessam / lararabbit example snippets


use AhmedEssam\LaraRabbit\Facades\RabbitMQ;

// Simple publish
RabbitMQ::publish('order.created', [
    'order_id' => 123,
    'customer_id' => 456,
    'amount' => 99.99
]);

// Publish with options
RabbitMQ::publish('order.created', $data, [
    'message_id' => $uuid,
    'priority' => 5,
    'headers' => ['source' => 'api']
]);

// Publish event with automatic metadata
RabbitMQ::publishEvent('OrderCreated', [
    'order_id' => 123,
    'customer_id' => 456,
    'amount' => 99.99
]);

// Batch publishing
$messages = [
    [
        'routingKey' => 'order.created',
        'data' => ['order_id' => 123]
    ],
    [
        'routingKey' => 'order.created',
        'data' => ['order_id' => 124]
    ]
];
RabbitMQ::publishBatch($messages);

use AhmedEssam\LaraRabbit\Facades\RabbitMQ;
use PhpAmqpLib\Message\AMQPMessage;

// Set up a queue and bind it to the exchange
RabbitMQ::setupQueue('orders_service', ['order.created', 'order.updated']);

// Consume messages from a queue
RabbitMQ::consume('orders_service', function ($data, AMQPMessage $message) {
    // Process the message data
    echo "Received message: " . print_r($data, true);
    
    // If you return false, the message will not be acknowledged
    return true;
});

// Set up a predefined queue from configuration
RabbitMQ::setupPredefinedQueue('order_service');

// Consume from a predefined queue
RabbitMQ::consumeFromPredefinedQueue('order_service', function ($data, $message) {
    // Process the message data
    Log::info("Processing order", $data);
    return true;
});

use AhmedEssam\LaraRabbit\Facades\RabbitMQ;

// Set up a queue with a dead letter queue for failed messages
RabbitMQ::setupDeadLetterQueue(
    'orders_service',
    'orders_service_failed',
    ['order.failed']
);

// Consume messages from the dead letter queue
RabbitMQ::consume('orders_service_failed', function ($data, $message) {
    // Process failed messages
    Log::error('Failed to process order', $data);
});

// Register a schema
app(MessageValidatorInterface::class)->registerSchema('order.created', [
    'order_id' => 'itMQ::publish('order.created', $data, [
    'schema' => 'order.created'
]);

// Set serialization format globally
RabbitMQ::setSerializationFormat('msgpack');

// Or use different formats for different messages
RabbitMQ::setSerializationFormat('json')->publish('order.created', $data);
RabbitMQ::setSerializationFormat('msgpack')->publish('inventory.updated', $data);

// Get the connection manager
$connectionManager = RabbitMQ::getConnectionManager();

// Close connection when done
RabbitMQ::closeConnection();

// Configure error handling options in config/rabbitmq.php
'consumer' => [
    'throw_exceptions' => false,      // Whether to throw exceptions during processing
    'reconnect_delay' => 5,           // Seconds to wait before reconnection
    'reconnect_max_retries' => 3,     // Maximum reconnection attempts
    'stop_on_critical_error' => false,// Whether to stop consumption on critical errors
    'requeue_on_error' => false,      // Whether to requeue failed messages
],

// Consumption will automatically handle reconnection
RabbitMQ::consume('orders_service', function ($data, $message) {
    try {
        // Process message
        return true; // Acknowledge on success
    } catch (\Exception $e) {
        // Failed messages will be handled according to configuration
        return false; // Reject the message
    }
});
bash
php artisan lararabbit:list-queues
bash
php artisan lararabbit:purge-queue orders_service