2017 © Pedro Peláez
 

library laravel-rabbitmq

Queuing, Messages exchange, etc

image

deankh/laravel-rabbitmq

Queuing, Messages exchange, etc

  • Friday, May 11, 2018
  • by DeanKH
  • Repository
  • 1 Watchers
  • 0 Stars
  • 151 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 175 % Grown

The README.md

RabbitMQ Queue driver for Laravel/Lumen 5.4+

Description

Laravel/Lumen wrapper for RabbitMQ queue (queue - worker) and messaging ( pub - sub). This package uses separate configuration options for the Laravel\Lumen queue extension, and another implementation for Pub\Sub., (*1)

Installation

  1. Install this package via composer using:, (*2)

    composer require deanhyde/laravel-rabbitmq, (*3)

  2. Add the Service Provider, (*4)

    • For Laravel use the providers section in the config/app.php file DeanKH\RabbitMQ\ServiceProvider::class,. Afterwards just publish the configuration using: php artisan vendor:publish --provider=DeanKH\RabbitMQ\ServiceProvider --tag=config, (*5)

    • For Lumen use $app->register(DeanKH\RabbitMQ\ServiceProvider::class) in your bootstrap/app.php file. After that you will have to create new file in your config folder: config/messaging.php and put the sample content from the messaging-sample.php file found in: vendor/kenokokoro/laravel-rabbitmq/config/messaging-sample.php. Finally just include this configuration file inside your bootstrap/app.php file using: $app->configure('messaging'), (*6)

  3. Even though the both implementations have different configuration, the connection configuration is same for both, (*7)

    QUEUE_DRIVER=rabbitmq
    RABBITMQ_HOST=127.0.0.1
    RABBITMQ_PORT=5672
    RABBITMQ_VHOST=/
    RABBITMQ_LOGIN=guest
    RABBITMQ_PASSWORD=guest

    List of available environment values can be found in: vendor/kenokokoro/laravel-rabbitmq/config/queue.php, (*8)

    NOTE: The environment configuration values are used only in the laravel queue extension. For the messaging (pub - sub) it is used different type of configuration, (*9)

Usage

  1. Queue (Laravel official documentation), (*10)

    • On Laravel: Queue::push(App\Jobs\DummyJob::class)
    • On Lumen you can use the same if you have $app->withFacades() added in your boostrap/app.php file, or simply app('queue')->push(App\Jobs\SomeJob::class)
    • To consume either of this just simply use the Laravel\Lumen queue worker: php artisan queue:work
  2. Messaging, (*11)

    The messaging is using different configuration for queue management (except for the rabbitmq connection). To get in touch for some examples of how the rabbitmq exchange and queue parameters are important check the RabbitMQ examples, (*12)

    1. Using dependency injection, (*13)

      Publishing example:, (*14)

      <?php
      namespace App\Console\Commands;
      use Illuminate\Console\Command;
      use DeanKH\RabbitMQ\Messaging\Pub\PublishInterface;
      use DeanKH\RabbitMQ\Messaging\Pub\Data;
      class PublishCommand extends Command
      {
          protected $signature = 'publish';
          /**
           * @var PublishInterface
           */
          private $publish;
      
          public function __construct(PublishInterface $publish)
          {
              parent::__construct();
              $this->publish = $publish;
          }
      
          public function handle()
          {
              $this->publish->route(['test.1', 'test.2', 'test.3'], str_random());
              # Or if you want to send array you can use the dedicated class
              # $this->publish->route(['test.1', 'test.2', 'test.3'], new Data(['hello' => 'world']);
          }
      }
      

      Consuming the publisher example:, (*15)

      <?php
      namespace App\Console\Commands;
      use Illuminate\Console\Command;
      use PhpAmqpLib\Message\AMQPMessage;
      use DeanKH\RabbitMQ\Messaging\Sub\ConsumeInterface;
      class DummyCommand extends Command
      {
          protected $signature = 'consume';
      
          /**
           * @var ConsumeInterface
           */
          private $consume;
      
          public function __construct(ConsumeInterface $consume)
          {
              parent::__construct();
              $this->consume = $consume;
          }
      
          public function handle()
          {
              $this->consume->route(['test.*'], function ($msg) {
                  return $this->msg($msg);
              });
          }
      
          private function msg(AMQPMessage $msg)
          {
              $this->line($msg->body);
          }
      }
      
    2. The same can be achieved using app(DeanKH\RabbitMQ\Messaging\Sub\ConsumeInterface::class) or app(DeanKH\RabbitMQ\Messaging\Pub\PublishInterface:class), (*16)

License

The Laravel\Lumen RabbitMQ package is open-sourced software licensed under the MIT license., (*17)

The Versions