PHP code example of infoesportes / messaging-rabbitmq
1. Go to this page and download the library: Download infoesportes/messaging-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/ */
infoesportes / messaging-rabbitmq example snippets
use InfoEsportes\Messaging\Publisher\RabbitMQPublisher;
use InfoEsportes\Messaging\Core\MessagePriority;
$config = [
'host' => '127.0.0.1',
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'vhost' => '/',
'exchange' => 'messages.topic',
];
$publisher = new RabbitMQPublisher($config, 'my-app');
// Send an email
$publisher->sendEmail(
'[email protected]',
'Welcome!',
'<h1>Welcome to our service</h1>',
[
'template' => 'welcome',
'variables' => ['name' => 'John'],
],
'email.transactional',
MessagePriority::HIGH
);
// Send an SMS
$publisher->sendSMS(
'+5511999999999',
'Your verification code is: 123456',
'sms.notification',
MessagePriority::URGENT
);
// Send a WhatsApp message
$publisher->sendWhatsApp(
'+5511999999999',
'Your order has been shipped!',
'whatsapp.notification',
'https://example.com/tracking.jpg',
MessagePriority::NORMAL
);
use InfoEsportes\Messaging\Consumer\RabbitMQConsumer;
use PhpAmqpLib\Message\AMQPMessage;
$config = [
'host' => '127.0.0.1',
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'vhost' => '/',
'exchange' => 'messages.topic',
'prefetch_count' => 10,
'auto_ack' => false, // Manual acknowledgment
];
$consumer = new RabbitMQConsumer($config);
// Declare a queue and bind routing keys
$consumer->declareQueue(
'email.queue',
['email.#'], // Routing keys to bind
true, // Durable
false, // Exclusive
false, // Auto-delete
10 // Max priority
);
// Start consuming
$consumer->consume('email.queue', function (AMQPMessage $msg, $consumer) {
$data = json_decode($msg->getBody(), true);
echo "Processing message: " . $data['id'] . "\n";
try {
// Process your message here
processEmail($data);
// Message is automatically acknowledged on success
} catch (\Exception $e) {
// On error, message is rejected and requeued
echo "Error: " . $e->getMessage() . "\n";
throw $e;
}
});
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use InfoEsportes\Messaging\Publisher\RabbitMQPublisher;
use InfoEsportes\Messaging\Consumer\RabbitMQConsumer;
class MessagingServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(RabbitMQPublisher::class, function ($app) {
return new RabbitMQPublisher([
'host' => config('rabbitmq.host'),
'port' => config('rabbitmq.port'),
'user' => config('rabbitmq.user'),
'password' => config('rabbitmq.password'),
'vhost' => config('rabbitmq.vhost'),
'exchange' => config('rabbitmq.exchange'),
], config('app.name'));
});
$this->app->singleton(RabbitMQConsumer::class, function ($app) {
return new RabbitMQConsumer([
'host' => config('rabbitmq.host'),
'port' => config('rabbitmq.port'),
'user' => config('rabbitmq.user'),
'password' => config('rabbitmq.password'),
'vhost' => config('rabbitmq.vhost'),
'exchange' => config('rabbitmq.exchange'),
'prefetch_count' => config('rabbitmq.prefetch_count', 10),
]);
});
}
}
namespace Config;
use CodeIgniter\Config\BaseService;
use InfoEsportes\Messaging\Publisher\RabbitMQPublisher;
use InfoEsportes\Messaging\Consumer\RabbitMQConsumer;
class Services extends BaseService
{
public static function messagePublisher($getShared = true)
{
if ($getShared) {
return static::getSharedInstance('messagePublisher');
}
$config = config('RabbitMQ');
return new RabbitMQPublisher($config->toArray(), 'my-app');
}
public static function messageConsumer($getShared = true)
{
if ($getShared) {
return static::getSharedInstance('messageConsumer');
}
$config = config('RabbitMQ');
return new RabbitMQConsumer($config->toArray());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.