3slab/vdm-library-amqp-transport-bundle
Install the Bundle Add the package via Composer in your Laravel project:
composer require 3slab/vdm-library-amqp-transport-bundle
Register the bundle in config/app.php under extra.bundles (if using Symfony-style bundles) or integrate via Laravel's service provider (if adapted).
Configure AMQP Connection
Publish the default config (if available) and update .env:
AMQP_HOST=localhost
AMQP_PORT=5672
AMQP_USER=guest
AMQP_PASSWORD=guest
AMQP_VHOST=/
Override default settings in config/packages/3slab_vdm_library_amqp_transport.yaml (if config exists) or manually bind the connection in Laravel’s config/amqp.php.
First Use Case: Sending a Message Inject the AMQP transport service into a Laravel service or controller:
use Vdm\Library\AmqpTransportBundle\Transport\AmqpTransport;
public function __construct(private AmqpTransport $amqpTransport) {}
public function sendMessage()
{
$this->amqpTransport->send(
exchange: 'my_exchange',
routingKey: 'my.routing.key',
payload: json_encode(['data' => 'test']),
options: ['content_type' => 'application/json']
);
}
Producers (Publishers)
AmqpTransport to publish messages to exchanges with routing keys.$this->amqpTransport->send(
exchange: 'events',
routingKey: 'user.created',
payload: $userData,
options: ['delivery_mode' => AMQP_MSG_PERSISTENT]
);
Consumers (Subscribers)
$consumer = new AmqpConsumer($this->amqpTransport, 'my_queue');
$consumer->consume(function ($message) {
// Process message (e.g., update DB, trigger Laravel jobs)
dispatch(new ProcessMessageJob($message->body));
$message->ack();
});
Error Handling
try-catch blocks around send()/consume() calls.try {
$this->amqpTransport->send(...);
} catch (\Exception $e) {
\Log::error("AMQP send failed: " . $e->getMessage());
}
Queue Workers
Illuminate\Queue\QueueManager or creating a custom driver.createAmqpConnection() in a custom driver.Event Dispatching
Event::listen(UserCreated::class, function ($event) {
$this->amqpTransport->send(
exchange: 'events',
routingKey: 'user.created',
payload: $event->user
);
});
Microservices Communication
UserService publishes UserUpdated events to AMQP, while another service consumes them.Connection Management
retry() helper or use a library like php-amqplib's Connection with reconnect flag.
$connection = new AMQPConnection($host, $port, $user, $pass, $vhost, false, 'guest', 0, AMQP_SSL_SILENT, null, null, ['reconnect' => true]);
Message Persistence
delivery_mode to AMQP_MSG_PERSISTENT in options:
$this->amqpTransport->send(..., ['delivery_mode' => AMQP_MSG_PERSISTENT]);
Routing Key Mismatches
direct, topic) or routing key doesn’t match.Bundle Maturity
php-amqplib dependency).Enable AMQP Debugging
AMQP_DEBUG in .env or enable php-amqplib logging:
AMQPConnection::setDebug(true); // If exposed by the bundle
Inspect Messages
Test Locally
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
Custom Transport Layer
AmqpTransport to add Laravel-specific features (e.g., job dispatching):
class LaravelAmqpTransport extends AmqpTransport
{
public function sendAsJob($job, string $exchange, string $routingKey)
{
$this->send($exchange, $routingKey, $job->serialize());
// Dispatch job to Laravel queue for processing
}
}
Middleware for Messages
$this->amqpTransport->addMiddleware(new JsonSerializeMiddleware());
Laravel Service Provider Integration
$this->app->bind(AmqpTransport::class, function ($app) {
return new AmqpTransport(
new AMQPConnection($app['config']['amqp.host'], ...)
);
});
How can I help you explore Laravel packages today?