enqueue/amqp-tools
AMQP Tools for PHP Enqueue: adds practical utilities not covered by the AMQP spec, built on top of AMQP concepts. Works with any amqp-interop compatible transport. Links to docs, support, and issue tracker included.
enqueue/amqp-tools package is ideal for Laravel applications requiring AMQP-based message queues (e.g., RabbitMQ) for background job processing, event publishing, or microservices communication. It aligns well with Laravel’s queue system (via queue:work) but extends functionality with direct AMQP control (e.g., custom exchanges, bindings, or advanced routing).schedule:run) and event system (events).database, redis, beanstalkd). Requires explicit AMQP setup.Bus, Dispatchable interfaces), so custom integration is needed for seamless job dispatching.Illuminate\Queue\QueueManager to support AMQP connections. Example:
$queueManager->extend('amqp', function ($app) {
return new AmqpQueue(new AmqpConnection($app['config']['amqp']));
});
Illuminate\Bus\Queueable). AMQP messages may need custom serialization (e.g., JSON) if using non-Laravel consumers.Event::dispatch() with direct AMQP publishing for high-throughput events (e.g., Event::listen() → amqp_producer->publish()).Illuminate\Queue\Middleware.| Risk Area | Mitigation Strategy |
|---|---|
| Connection Management | Use Laravel’s service container to manage AMQP connections (singleton pattern). |
| Message Serialization | Standardize on JSON or Laravel’s serialize() for cross-language compatibility. |
| Error Handling | Implement dead-letter exchanges (DLE) in AMQP for failed messages. |
| Performance Overhead | Benchmark against Laravel’s default queues (e.g., Redis) for latency/cost tradeoffs. |
| Vendor Lock-in | Abstract AMQP logic behind interfaces (e.g., MessageProducer, MessageConsumer) for future flexibility. |
env() or AWS Secrets Manager).php-amqplib’s MockConnection).queue:work with AMQP-backed consumers (e.g., php artisan queue:work --queue=amqp).Artisan::command('process-orders')->handle() → AMQP job).php-amqplib/php-amqplib: Required for AMQP connectivity (handled as a dependency by enqueue/amqp-tools).enqueue/amqp-ext: Optional for AMQP 1.0 support (if using modern brokers like RabbitMQ 3.10+).pika, Node.js amqplib) without Laravel overhead.redis) with AMQP for a non-critical workflow (e.g., sending emails).QueueManager extension to support AMQP alongside existing drivers.Event::dispatch() with direct AMQP publishing for high-volume events.prefetch_count, message_ttl) based on load testing.| Component | Compatibility Notes |
|---|---|
| Laravel 10/11 | Works with modern Laravel (tested with php-amqplib v2.0+). |
| PHP 8.1+ | Requires PHP 8.1+ for enqueue/amqp-tools (check composer.json). |
| RabbitMQ | Tested with RabbitMQ 3.8+ (AMQP 0-9-1). For AMQP 1.0, use enqueue/amqp-ext. |
| Other Brokers | Primarily RabbitMQ; limited support for Apache Qpid or CloudAMQP. |
| Laravel Jobs | Jobs must implement ShouldQueue and be serializable. Custom serialization may be needed. |
laravel.jobs, laravel.events).config/queue.php:
'connections' => [
'amqp' => [
'driver' => 'amqp',
'host' => env('AMQP_HOST'),
'port' => env('AMQP_PORT', 5672),
'user' => env('AMQP_USER'),
'password' => env('AMQP_PASSWORD'),
'vhost' => env('AMQP_VHOST', '/'),
'queue' => env('AMQP_QUEUE', 'laravel'),
'exchange' => env('AMQP_EXCHANGE', 'laravel'),
'options' => [
'prefetch_count' => env('AMQP_PREFETCH', 10),
],
],
],
php artisan make:command AmqpWorker
public function handle() {
$connection = new AmqpConnection(config('queue.amqp'));
$consumer = new AmqpConsumer($connection, config('queue.amqp.queue'));
$consumer->consume(function (Message $message) {
$job = unserialize($message->body);
$job->handle();
$message->ack();
});
}
dispatch() as usual, but ensure the amqp queue is configured in the job’s queue property.ack/nack rates).How can I help you explore Laravel packages today?