1. Go to this page and download the library: Download opsource/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/ */
opsource / rabbitmq example snippets
$rabbitMQ = new RabbitMQManager(app());
$rabbitMQ = app('rabbitmq');
class YourClass {
protected $rabbitMQ;
// Constructor method
public function __construct(RabbitMQManager $rabbitMQ) {
$this->rabbitMQ = $rabbitMQ;
}
// Other methods...
}
$connections = RabbitMQ::getConnections();
use App\Services\RabbitMQManager;
use Illuminate\Support\Facades\RabbitMQ;
class YourService {
protected $rabbitMQ;
public function __construct(RabbitMQManager $rabbitMQ) {
$this->rabbitMQ = $rabbitMQ;
}
public function handle() {
// Retrieve all RabbitMQ connections
$connections = RabbitMQ::getConnections();
// Perform operations using the $rabbitMQ instance
// Example: $this->rabbitMQ->publishMessage($message);
}
}
// Usage in a controller or another service
$service = new YourService(app('rabbitmq'));
$service->handle();
$message = new RabbitMQMessage('message body');
$messages = [
new RabbitMQMessage('message 1'),
new RabbitMQMessage('message 2')
];
$rabbitMQ->publisher()->publish($messages);
$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
$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);