Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Amqp Interop Laravel Package

queue-interop/amqp-interop

Set of PHP interfaces and contracts for AMQP messaging interoperability. Standardizes producers, consumers, contexts and messages so different AMQP client libraries and queue implementations can be swapped without changing application code.

Deep Wiki
Context7

Getting Started

First Steps

  1. Installation Add the package via Composer:

    composer require queue-interop/amqp-interop
    

    Ensure your Laravel project uses PHP 7.1+ (compatible with the package).

  2. Basic Setup Register the AMQP transport in Laravel’s config/queue.php:

    'connections' => [
        'amqp' => [
            'driver' => 'amqp',
            'host' => env('AMQP_HOST', 'localhost'),
            'port' => env('AMQP_PORT', 5672),
            'user' => env('AMQP_USER', 'guest'),
            'password' => env('AMQP_PASSWORD', 'guest'),
            'vhost' => env('AMQP_VHOST', '/'),
            'queue' => env('AMQP_QUEUE', 'laravel'),
            'options' => [
                'ssl' => env('AMQP_SSL', false),
                'read_write_timeout' => 15,
            ],
        ],
    ],
    
  3. First Use Case: Dispatching a Job Create a job (e.g., SendEmailJob) and dispatch it to the AMQP queue:

    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    
    class SendEmailJob implements ShouldQueue
    {
        use Dispatchable, Queueable;
    
        public function handle()
        {
            // Job logic
        }
    }
    
    // Dispatch
    SendEmailJob::dispatch();
    
  4. Verify Queue Connection Run the queue worker:

    php artisan queue:work amqp --queue=laravel
    

Implementation Patterns

Common Workflows

  1. Queue Configuration

    • Dynamic Queues: Use env() to switch queues per environment (e.g., dev, staging, prod).
    • Exchange Binding: Leverage AMQP exchanges for routing (e.g., fanout, direct, topic):
      $connection = app('amqp.connection');
      $channel = $connection->channel();
      $channel->exchange_declare('logs', 'fanout', false, false, false);
      $channel->queue_declare('logs_queue', false, false, false, false);
      $channel->queue_bind('logs_queue', 'logs');
      
  2. Job Prioritization

    • Use AMQP’s priority queues (if supported by your broker):
      SendEmailJob::dispatch()->onQueue('high_priority')->priority(10);
      
  3. Delayed Jobs

    • Schedule jobs with delay():
      SendEmailJob::dispatch()->delay(now()->addMinutes(10));
      
    • For AMQP-specific delays, use TTL (Time-To-Live) on queues or dead-letter exchanges.
  4. Consumer Groups

    • Scale workers by binding multiple consumers to the same queue:
      php artisan queue:work amqp --queue=laravel --daemon
      
  5. Publish/Subscribe

    • Publish events to exchanges and consume them in workers:
      // Publisher (e.g., in a controller)
      $connection->channel()->basic_publish(
          '', 'logs', new AMQPMessage('Event data')
      );
      
      // Consumer (in a worker)
      $channel->consume('logs_queue', function ($msg) {
          // Handle message
      }, ['no_ack' => false]);
      

Integration Tips

  • Laravel Events: Bridge AMQP with Laravel’s event system:
    use Illuminate\Support\Facades\Event;
    
    Event::listen('order.created', function ($order) {
        $connection->channel()->basic_publish(
            '', 'orders', new AMQPMessage(json_encode($order))
        );
    });
    
  • Retry Logic: Customize retry behavior in app/Exceptions/Handler.php:
    public function register()
    {
        $this->reportable(function (JobFailedException $e) {
            if ($e->job instanceof SendEmailJob) {
                // Log or alert for critical failures
            }
        });
    }
    
  • Monitoring: Use php-amqplib’s built-in metrics or integrate with tools like Prometheus via middleware.

Gotchas and Tips

Pitfalls

  1. Connection Management

    • Issue: AMQP connections can hang or time out under load.
    • Fix: Use connection pooling or reconnect logic:
      try {
          $channel = $connection->channel();
      } catch (AMQPConnectionException $e) {
          $connection = new AMQPConnection($host, $port, $user, $pass);
          $channel = $connection->channel();
      }
      
    • Tip: Set read_write_timeout in config to avoid indefinite hangs.
  2. Message Serialization

    • Issue: AMQP messages must be serialized/deserialized manually (unlike Laravel’s default JSON).
    • Fix: Use json_encode()/json_decode() or a library like spatie/array-to-object:
      $message = new AMQPMessage(json_encode($data));
      $data = json_decode($channel->basic_get()->body, true);
      
  3. Ack/Nack Behavior

    • Issue: Unhandled exceptions may cause messages to be lost or redelivered indefinitely.
    • Fix: Explicitly ack/nack messages in consumers:
      $channel->consume('queue', function ($msg) {
          try {
              // Process
              $msg->ack();
          } catch (\Exception $e) {
              $msg->nack(false, false, 5); // Requeue after 5 retries
          }
      });
      
  4. Queue Visibility

    • Issue: Long-running jobs may block other consumers.
    • Fix: Use basic_get() with no_ack: false and manually ack after processing.
  5. SSL/TLS Issues

    • Issue: Self-signed certificates or missing CA bundles can break connections.
    • Fix: Configure SSL options in config/queue.php:
      'options' => [
          'ssl' => [
              'local_cert' => env('AMQP_SSL_CERT'),
              'passphrase' => env('AMQP_SSL_PASSPHRASE'),
              'verify_peer' => false, // Disable for testing only!
          ],
      ],
      

Debugging Tips

  • Enable AMQP Debugging:
    $connection->setReadWriteTimeout(15);
    $connection->setDebug(true); // Logs raw AMQP traffic
    
  • Check Queue State: Use php-amqplib's CLI tools or GUI tools like RabbitMQ Management Plugin to inspect queues/exchanges.
  • Log Consumed Messages:
    $channel->consume('queue', function ($msg) {
        logger()->debug('Received:', ['body' => $msg->body]);
    });
    

Extension Points

  1. Custom Transports Extend Illuminate\Queue\AmqpQueue to add broker-specific logic:
    class CustomAmqpQueue extends AmqpQueue
    {
        public function pushRaw($job, $data, $queue = null)
        {
            // Custom logic (e.g., set headers)
            $message = new AMQPMessage($data, [
                'app_id' => 'laravel',
                'priority' => $job->priority,
            ]);
            $this->publish($message, $queue);
        }
    }
    
  2. Middleware Add AMQP-specific middleware to jobs:
    class LogAmqpJobMiddleware
    {
        public function handle($job, $next)
        {
            logger()->info('AMQP Job dispatched', ['job' => get_class($job)]);
            return $next($job);
        }
    }
    
    Register in app/Console/Kernel.php:
    protected function schedule(Schedule $schedule)
    {
        $schedule->job(SendEmailJob::class)
                ->middleware(LogAmqpJobMiddleware::class);
    }
    
  3. Event-Driven Extensions Listen to AMQP events (e.g., amqp.message in Laravel 8+) via service providers:
    public function boot()
    {
        event(new AMQPMessageEvent($message));
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4