PHP code example of kunalvarma05 / laravel-rabbitmq
1. Go to this page and download the library: Download kunalvarma05/laravel-rabbitmq library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kunalvarma05 / laravel-rabbitmq example snippets
// A. Direct instantiation
$rabbitMQ = new RabbitMQManager(app());
// B. Binding
$rabbitMQ = app('rabbitmq');
// C. Dependency injection (Controller, Command, Job, etc.)
public function __consturct(RabbitMQManager $rabbitMQ) { ... }
// D. Facade
// All the public methods of the `RabbitMQManager` class
// are available through the `RabbitMQ` facade.
RabbitMQ::getConnections();
$message = new RabbitMQMessage('message body');
// Publish to the default exchange/topic/queue
$rabbitMQ->publisher()->publish($message);
// Publish bulk messages
$messages = [new RabbitMQMessage('message 1'), new RabbitMQMessage('message 2')];
$rabbitMQ->publisher()->publish($messages);
// A. Consume through a closure
$handler = new RabbitMQGenericMessageConsume(function (RabbitMQIncomingMessage $message) {
$content = $message->getStream();
});
// B. Consume through a class
class MyMessageConsumer extends RabbitMQMessageConsumer {
public function handle(RabbitMQIncomingMessage $message) {
$content = $message->getStream();
}
}
$handler = new MyMessageConsumer();
// Starts a blocking loop `while (true)`
$rabbitMQ->consumer()->consume($handler);
$publisher = $rabbitMQ->publisher();
$message = new RabbitMQMessage('message body');
$exchangeConfig = ['type' => AMQPExchangeType::TOPIC];
$exchange = new RabbitMQExchange('my_exchange', $exchangeConfig);
$message->setExchange($exchange);
$routingKey = 'key'; // Can be an empty string, but not null
$connectionName = 'custom_connection'; // Set to null for default connection
// The publish config allows you to any override default configuration
//
// The following precendence works for the configuration:
// Message exchange config > Publish config > Connection config > Default config
//
// In this case, the exchange type used would be AMQPExchangeType::TOPIC
$publishConfig = new PublishConfig(['exchange' => ['type' => AMQPExchangeType::FANOUT]]);
$publisher->publish($message, $routingKey, $connectionName, $publishConfig);
$consumer = $rabbitMQ->consumer();
$routingKey = 'key';
$exchange = new RabbitMQExchange('test_exchange', ['declare' => true, 'durable' => true]);
$queue = new RabbitMQQueue('my_queue', ['declare' => true, 'durable' => true]);
$messageConsumer = new RabbitMQGenericMessageConsumer(
function (RabbitMQIncomingMessage $message) {
// Acknowledge a message
$message->getDelivery()->acknowledge();
// Reject a message
$requeue = true; // Reject and Requeue
$message->getDelivery()->reject($requeue);
},
$this,
);
// A1. Set the exchange and the queue directly
$messageConsumer
->setExchange($exchange)
->setQueue($queue);
// OR
// A2. Set the exchange and the queue through config
$consumeConfig = new ConsumeConfig(
[
'queue' => [
'name' => 'my_queue',
'declare' => true,
'durable' => true,
],
'exchange' => [
'name' => 'test_exchange',
'declare' => true,
],
],
);
$consumer->consume(
$messageConsumer,
$routingKey,
null,
$consumeConfig,
);
class MyRabbitMQController extends Controller {
public function publish(Request $request)
{
$rabbitMQ = app('rabbitmq');
$consumer = $rabbitMQ->consumer();
$routingKey = 'key'; // The key used by the consumer
// The exchange (name) used by the consumer
$exchange = new RabbitMQExchange('test_exchange', ['declare' => true]);
$contents = $request->get('message', 'random message');
$message = new RabbitMQMessage($contents);
$message->setExchange($exchange);
$rabbitMQ->publisher()->publish(
$message,
$routingKey
);
return ['message' => "Published {$contents}"];
}
}
composer run-script test
sh
php artisan vendor:publish --tag=config
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.