PHP code example of kuncen / mcs-laravel-rabbitmq

1. Go to this page and download the library: Download kuncen/mcs-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/ */

    

kuncen / mcs-laravel-rabbitmq example snippets


use Kuncen\MCSLaravel\RabbitMQ\Services\RabbitMQPublisher;

class UserController extends Controller
{
    public function store(RabbitMQPublisher $publisher)
    {
        $publisher->publish(
            payload: [
                'user_id' => 1,
                'name' => 'John Doe'
            ],
            routingKey: 'user.created'
        );

        return response()->json(['status' => 'Message sent']);
    }
}

publish(
    array $payload,
    string $routingKey,
    string $exchangeType = 'topic',
    ?string $exchange = null
)

class UserCreatedListener implements RabbitMQListener
{
    public static function queue(): string
    {
        return 'user.created';
    }

    public function handle(array $payload): void
    {
        // Your business logic here
    }
}

use Kuncen\MCSLaravel\RabbitMQ\Services\RabbitMQPublisher;

class UserController extends Controller
{
    public function store(RabbitMQPublisher $publisher)
    {
        $publisher->publish(
            payload: [
                'user_id' => 1,
                'name' => 'John Doe'
            ],
            routingKey: 'user.created'
        );

        return response()->json(['status' => 'Message sent']);
    }
}

publish(
    array $payload,
    string $routingKey,
    string $exchangeType = 'topic',
    ?string $exchange = null
)

class UserCreatedListener implements RabbitMQListener
{
    public static function queue(): string
    {
        return 'user.created';
    }

    public function handle(array $payload): void
    {
        // Logic bisnis di sini
    }
}
bash
php artisan vendor:publish --provider="Kuncen\MCSLaravel\RabbitMQ\RabbitMQServiceProvider"

config/rabbitmq.php
bash
php artisan make:rabbitlistener UserCreatedListener
bash
php artisan rabbitmq:consume UserCreatedListener
bash
php artisan vendor:publish --provider="Kuncen\McsRabbitMQ\RabbitMQServiceProvider"

config/rabbitmq.php
bash
php artisan make:rabbitlistener UserCreatedListener
bash
php artisan rabbitmq:consume UserCreatedListener