|
1 |
| -# LaravelRabbitMQ |
2 |
| -LaravelRabbitMQ |
| 1 | +# RabbitMQ Queue driver for Laravel |
| 2 | + |
| 3 | + |
| 4 | +## Support Policy |
| 5 | + |
| 6 | +Only the latest version will get new features. Bug fixes will be provided using the following scheme: |
| 7 | + |
| 8 | +| Package Version | Laravel Version | PHP Version | Bug Fixes Until | | |
| 9 | +|-----------------|-----------------|-------------|-------------------|-------------------------------------------------------------------------------------| |
| 10 | +| 0 | 9 | ^8.1 | August 26th, 2023 | [Documentation](https://github.com/iamfarhad/LaravelRabbitMQ/blob/master/README.md) | |
| 11 | + |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +You can install this package via composer using this command: |
| 16 | + |
| 17 | +``` |
| 18 | +composer require iamfarhad/laravel-rabbitmq |
| 19 | +``` |
| 20 | + |
| 21 | +The package will automatically register itself. |
| 22 | + |
| 23 | +Add connection to config/queue.php: |
| 24 | + |
| 25 | +```php |
| 26 | + 'connections' => [ |
| 27 | + // ..... |
| 28 | + 'rabbitmq' => [ |
| 29 | + 'driver' => 'rabbitmq', |
| 30 | + 'queue' => env('RABBITMQ_QUEUE', 'default'), |
| 31 | + |
| 32 | + 'hosts' => [ |
| 33 | + 'host' => env('RABBITMQ_HOST', '127.0.0.1'), |
| 34 | + 'port' => env('RABBITMQ_PORT', 5672), |
| 35 | + 'user' => env('RABBITMQ_USER', 'guest'), |
| 36 | + 'password' => env('RABBITMQ_PASSWORD', 'guest'), |
| 37 | + 'vhost' => env('RABBITMQ_VHOST', '/'), |
| 38 | + 'lazy' => env('RABBITMQ_LAZY_CONNECTION', true), |
| 39 | + 'keepalive' => env('RABBITMQ_KEEPALIVE_CONNECTION', false), |
| 40 | + 'heartbeat' => env('RABBITMQ_HEARTBEAT_CONNECTION', 0), |
| 41 | + ], |
| 42 | + |
| 43 | + 'options' => [ |
| 44 | + 'ssl_options' => [ |
| 45 | + 'cafile' => env('RABBITMQ_SSL_CAFILE', null), |
| 46 | + 'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null), |
| 47 | + 'local_key' => env('RABBITMQ_SSL_LOCALKEY', null), |
| 48 | + 'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true), |
| 49 | + 'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null), |
| 50 | + ], |
| 51 | + 'queue' => [ |
| 52 | + 'job' => \iamfarhad\LaravelRabbitMQ\Jobs\RabbitMQJob::class, |
| 53 | + ], |
| 54 | + ], |
| 55 | + ] |
| 56 | + ] |
| 57 | +``` |
| 58 | + |
| 59 | +## Laravel Usage |
| 60 | + |
| 61 | +Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to |
| 62 | +change anything else. If you do not know how to use Queue API, please refer to the official Laravel |
| 63 | +documentation: http://laravel.com/docs/queues |
| 64 | + |
| 65 | +## Lumen Usage |
| 66 | + |
| 67 | +For Lumen usage the service provider should be registered manually as follows in `bootstrap/app.php`: |
| 68 | + |
| 69 | +```php |
| 70 | +$app->register(iamfarhad\LaravelRabbitMQ\LaravelRabbitQueueServiceProvider::class); |
| 71 | +``` |
| 72 | + |
| 73 | +## Consuming Messages |
| 74 | + |
| 75 | +There are two ways of consuming messages. |
| 76 | + |
| 77 | +1. `queue:work` command which is Laravel's built-in command. This command utilizes `basic_get`. |
| 78 | + |
| 79 | +2. `rabbitmq:consume` command which is provided by this package. This command utilizes `basic_consume` and is more |
| 80 | + performant than `basic_get` by ~3x. |
| 81 | + |
| 82 | +```shell |
| 83 | + php artisan rabbitmq:consume --queue=customQueue |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | +You can create jobs with custom queue same as below |
| 88 | + |
| 89 | +```php |
| 90 | +class TestJob implements ShouldQueue |
| 91 | +{ |
| 92 | + use Dispatchable; |
| 93 | + use InteractsWithQueue; |
| 94 | + use Queueable; |
| 95 | + use SerializesModels; |
| 96 | + |
| 97 | + public function __construct() |
| 98 | + { |
| 99 | + $this->onQueue('customQueue'); |
| 100 | + } |
| 101 | + |
| 102 | + public function handle() |
| 103 | + { |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +``` |
0 commit comments