PHP code example of edipoelwes / laravel-rabbitmq-worker
1. Go to this page and download the library: Download edipoelwes/laravel-rabbitmq-worker 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/ */
edipoelwes / laravel-rabbitmq-worker example snippets
return [
'connections' => [
'host' => env('RABBITMQ_HOST', 'localhost'),
'port' => env('RABBITMQ_PORT', 5672),
'user' => env('RABBITMQ_LOGIN', 'guest'),
'password' => env('RABBITMQ_PASSWORD', 'guest'),
'vhost' => env('RABBITMQ_VHOST', '/'),
]
];
use Edipoelwes\LaravelRabbitmqWorker\Services\RabbitMQ\RabbitMQService;
$rabbitMQService = new RabbitMQService(
'queue', // Queue
'route-key', // Routing key
'', // Exchange
'', // Exchange Type
'', // Consumer Tag
false, // Passive
true, // Durable
false, // Exclusive
false // Auto delete
);
// Prepare response payload
$payload = "your message";
$rabbitMQService->publish($payload);
$rabbitMQService->destruct(); // Clean up resources
use Edipoelwes\LaravelRabbitmqWorker\Services\RabbitMQ\RabbitMQService;
$rabbitMQService = new RabbitMQService(
'queue', // Queue
'route-key', // Routing key
'', // Exchange
'', // Exchange Type
'', // Consumer Tag
false, // Passive
true, // Durable
false, // Exclusive
false // Auto delete
);
$callback = function ($msg) {
// $msg->body
// your code here
$msg->ack();
}
$rabbitMQService->consume($callback);
$rabbitMQService->destruct(); // Clean up resources
namespace App\Console\Commands;
use Edipoelwes\LaravelRabbitmqWorker\Infrastructure\Queue\Rabbitmq\QueueConsumerAbstract;
use Illuminate\Support\Facades\Log;
class RabbitmqTesting extends QueueConsumerAbstract
{
protected $signature = 'hello-command';
protected string $queueName = 'hello';
protected string $routeKey = 'hello';
protected string $consumerTag = 'rabbitmq-command-testing';
/**
* @throws \Exception
*/
public function process($message): void
{
Log::info('start handling');
Log::info($message->body);
$message->ack();
Log::info('finish handling');
}
}
namespace App\Console\Commands;
use Edipoelwes\LaravelRabbitmqWorker\Services\RabbitMQ\RabbitMQService;
use Illuminate\Console\Command;
class Consume extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'your-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$rabbitMQService = new RabbitMQService(
'queue', // Queue
'route-key', // Routing key
'', // Exchange
'', // Exchange Type
'', // Consumer Tag
false, // Passive
true, // Durable
false, // Exclusive
false // Auto delete
);
$callback = function ($msg) {
// $msg->body
// your code here
$msg->ack();
}
$rabbitMQService->consume($callback);
$rabbitMQService->destruct(); // Clean up resources
}
}
use Edipoelwes\LaravelRabbitmqWorker\Services\RabbitMQ\RabbitMQService;
$rabbitMQService = new RabbitMQService(
'rpc_queue', // Queue
'rpc_queue', // Routing key
'', // Exchange
'', // Exchange Type
'rpc_queue', // Consumer Tag
false, // Passive
false, // Durable
false, // Exclusive
true // Auto delete
);
// Define RPC Consumer Callback
$rabbitMQService->consume(function ($req) {
// Your code here
// Prepare response payload
$payload = "your message";
// Publish response to the specified channel and correlation ID
$msg = new AMQPMessage(
$payload,
array('correlation_id' => $req->get('correlation_id'))
);
$req->getChannel()->basic_publish(
$msg,
'',
$req->get('reply_to')
);
$req->ack(); // Acknowledge message processing
});
$rabbitMQService->destruct(); // Clean up resources
// Configure RabbitMQ for RPC Publisher
$rabbitMQService = new RabbitMQService(
'rpc_queue', // Queue
'rpc_queue', // Routing key
'', // Exchange
'', // Exchange Type
'rpc_queue', // Consumer Tag
false, // Passive
false, // Durable
false, // Exclusive
true // Auto delete
);
// Prepare response payload
$payload = "your message";
$response = $rabbitMQService->publishRpc($payload);
// your code here
$rabbitMQService->destruct(); // Clean up resources
bash
php artisan vendor:publish --provider="Edipoelwes\LaravelRabbitmqWorker\CommandServiceProvider"
bash
php artisan config:cache