PHP code example of djereg / laravel-rabbitmq

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

    

djereg / laravel-rabbitmq example snippets


# app/Events/UserCreated.php

namespace App\Events;

use Djereg\Laravel\RabbitMQ\Events\MessagePublishEvent;

class UserCreated extends MessagePublishEvent
{
    // Set the event name
    protected string $event = 'user.created';

    public function __construct(private User $user)
    {
        $this->user = $user;
    }

    // Create a payload method that returns the data to be sent
    public function payload(): array
    {
        return [
            'user_id' => $this->user->id,
        ];
    }
}

event(new UserCreated($user));

# app/Listeners/NotifyUser.php

namespace App\Listeners;

use Djereg\Laravel\RabbitMQ\Listeners\MessageEventListener;

class NotifyUser extends MessageEventListener {

    // Specify the events you want to listen to.
    // You can listen to multiple events by adding them to the array.
    public static array $listen = [
        'user.created',

        // 'user.updated',
        // 'user.deleted',
        // etc
    ];

    // The method that will be called when the event is received.
    // The event object is passed as an argument containing the event name and payload.
    protected function onEvent(MessageEvent $event): void {

    }

    // You can also define separate methods for each event.
    // The method name must be in the format on{EventName} where {EventName}
    // is the StudlyCase format of the event defined in the $listen property.
    // When both methods are defined, the on{EventName} method will be called.
    protected function onUserCreated(MessageEvent $event): void {
        //
    }

    // If no on{EventName} or onEvent method is defined, an exception will be thrown.
}

# app/Listeners/NotifyUser.php

namespace App\Listeners;

use Djereg\Laravel\RabbitMQ\Listeners\MessageEventListener;

class NotifyUser extends MessageEventListener implements ShouldQueue
{
    // Listener content
}

# app/Providers/EventServiceProvider.php

namespace App\Providers;

use Djereg\Laravel\RabbitMQ\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    // Provider content
}

# app/Providers/AppServiceProvider.php

namespace App\Providers;

use Djereg\Laravel\RabbitMQ\Services\Client;

class AppServiceProvider extends ServiceProvider {

    public function register(): void
    {
        $this->app->singleton(UserService::class, function($app) {

            // Instantiate the client with the remote service name and the queue connection
            $client = new Client('users', $app['queue.connection'])

            // Inject the client into the service
            return new UserService($client);
        });
    }
}

$client = new Client('users', app('queue.connection'));

# app/Services/UserService.php

namespace App\Services;

use Djereg\Laravel\RabbitMQ\Services\Client;

class UserService {

    public function __construct(private Client $users) {}

    public function getUser(int $id): mixed
    {
        // Call the remote procedure
        $user = $this->users->call('get', ['id' => $id]);

        // Process the response and return it
    }
}

# app/Providers/ProcedureServiceProvider.php

namespace App\Providers;

use Djereg\Laravel\RabbitMQ\Providers\ProcedureServiceProvider as ServiceProvider;

class ProcedureServiceProvider extends ServiceProvider
{
    //
}

# app/Procedures/GetUser.php

namespace App\Procedures;

use Djereg\Laravel\RabbitMQ\Procedures\Procedure;

class GetUser extends Procedure {

    // Set the procedure name that will be called by the client
    public static string $name = 'get';

    public function __invoke(int $id): mixed
    {
        // Get the user from the database and return it
    }

    // OR

    public function handle(int $id): mixed
    {
        // Get the user from the database and return it
    }
}

use Djereg\Laravel\RabbitMQ\Events\MessagePublishing;

use Djereg\Laravel\RabbitMQ\Events\MessagePublished;

use Djereg\Laravel\RabbitMQ\Events\MessageReceived;

use Djereg\Laravel\RabbitMQ\Events\MessageProcessed;
bash
php artisan rabbitmq:consume