Wrapper to php-amqplib library for publishing and consuming RabbitMQ messages using Laravel framework
PHP8.0 support will be available after php-amqplib is updated to the next major version 3.0. (php-amqplib/php-amqplib#858)
Laravel | Rabbit Client |
---|---|
6.x | 1.x |
7.x | 2.x |
8.x | -- |
$ composer require vladmeh/rabbit-client
or add the following to your requirement part within the composer.json:
{
"require": {
"vladmeh/rabbitmq-client": "^2.*"
}
}
Laravel will automatically register service provider (Vladmeh\RabbitMQ\RabbitMQClientProvider) and facade when is installed
Add these properties to .env with proper values:
RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
If you need advanced configuration properties run:
$ php artisan vendor:publish --tag=rabbit
This command will create a config file \config\rabbit.php
Rabbit::publish('message', '', 'queue-name');
Rabbit::publish('message', 'exchange-name', 'routing-key');
Rabbit::publish('message', 'amq.fanout', '', [
'hosts' => [
'vhosts' => 'vhost3'
],
'message' => [
'content_type' => 'application/json',
],
'exchange_declare' => [
'type' => 'fanout',
'auto_delete' => true,
]
]);
All default settings are defined in
\config\rabbit.php
.
Rabbit::consume('queue-name', function (AMQPMessage $msg) {
$msg->ack();
var_dump($msg->body);
if ($msg->getMessageCount() === null) {
$msg->getChannel()->basic_cancel($msg->getConsumerTag());
}
});
$response = Rabbit::rpc('message', 'queue-name', ['connection' => [
'read_write_timeout' => 10.0,
'channel_rpc_timeout' => 10.0
]]);
var_dump($response);