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

Vdm Library Amqp Transport Bundle Laravel Package

3slab/vdm-library-amqp-transport-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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).

  2. 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.

  3. 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']
        );
    }
    

Implementation Patterns

Workflow: Producer-Consumer Integration

  1. Producers (Publishers)

    • Use AmqpTransport to publish messages to exchanges with routing keys.
    • Example: Dispatching events or job payloads to a queue.
      $this->amqpTransport->send(
          exchange: 'events',
          routingKey: 'user.created',
          payload: $userData,
          options: ['delivery_mode' => AMQP_MSG_PERSISTENT]
      );
      
  2. Consumers (Subscribers)

    • Register a consumer class (if the bundle supports it) or manually consume messages via a Laravel queue worker.
    • Example: Using a Symfony-style consumer (adapt to Laravel):
      $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();
      });
      
    • Laravel Integration: Wrap the consumer in a Laravel command or queue listener.
  3. Error Handling

    • Use try-catch blocks around send()/consume() calls.
    • Log failures with Laravel’s logging:
      try {
          $this->amqpTransport->send(...);
      } catch (\Exception $e) {
          \Log::error("AMQP send failed: " . $e->getMessage());
      }
      

Patterns for Laravel-Specific Use Cases

  1. Queue Workers

    • Replace Laravel’s default queue drivers with AMQP by extending Illuminate\Queue\QueueManager or creating a custom driver.
    • Example: Override createAmqpConnection() in a custom driver.
  2. Event Dispatching

    • Listen for Laravel events and publish them to AMQP:
      Event::listen(UserCreated::class, function ($event) {
          $this->amqpTransport->send(
              exchange: 'events',
              routingKey: 'user.created',
              payload: $event->user
          );
      });
      
  3. Microservices Communication

    • Use AMQP for cross-service communication (e.g., publish domain events, consume commands).
    • Example: A UserService publishes UserUpdated events to AMQP, while another service consumes them.

Gotchas and Tips

Pitfalls

  1. Connection Management

    • Issue: AMQP connections may drop or time out. The bundle may not handle reconnection automatically.
    • Fix: Implement a retry mechanism with Laravel’s 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]);
      
  2. Message Persistence

    • Issue: Non-persistent messages may be lost if the broker restarts.
    • Fix: Set delivery_mode to AMQP_MSG_PERSISTENT in options:
      $this->amqpTransport->send(..., ['delivery_mode' => AMQP_MSG_PERSISTENT]);
      
  3. Routing Key Mismatches

    • Issue: Messages may go unrouted if the exchange type (e.g., direct, topic) or routing key doesn’t match.
    • Fix: Verify exchange declarations and routing keys match consumer bindings.
  4. Bundle Maturity

    • Issue: Last release in 2020; may lack Laravel 10+ compatibility.
    • Fix: Check for forks or adapt the bundle manually (e.g., update php-amqplib dependency).

Debugging Tips

  1. Enable AMQP Debugging

    • Set AMQP_DEBUG in .env or enable php-amqplib logging:
      AMQPConnection::setDebug(true); // If exposed by the bundle
      
    • Check Laravel logs for AMQP-related errors.
  2. Inspect Messages

    • Use a tool like [RabbitMQ Management Plugin](https://www.rabbitmq.com management) to verify messages are published/consumed.
  3. Test Locally

    • Spin up a RabbitMQ container for testing:
      docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
      

Extension Points

  1. Custom Transport Layer

    • Extend 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
          }
      }
      
  2. Middleware for Messages

    • Add middleware to transform messages before sending/receiving (e.g., serialization, validation):
      $this->amqpTransport->addMiddleware(new JsonSerializeMiddleware());
      
  3. Laravel Service Provider Integration

    • Bind the transport to Laravel’s IoC container for easier dependency injection:
      $this->app->bind(AmqpTransport::class, function ($app) {
          return new AmqpTransport(
              new AMQPConnection($app['config']['amqp.host'], ...)
          );
      });
      
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope