PHP code example of wzaradzki / laravel-microservice-communicator

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

    

wzaradzki / laravel-microservice-communicator example snippets


use WZaradzki\MicroserviceCommunicator\MicroserviceCommunicationManager;

class YourService
{
    public function __construct(
        private readonly MicroserviceCommunicationManager $communicator
    ) {}

    public function sendMessage(): bool
    {
        return $this->communicator->publish('topic-name', [
            'event' => 'UserCreated',
            'data' => [
                'id' => 1,
                'email' => '[email protected]'
            ]
        ]);
    }

    public function listenForMessages(): void
    {
        $this->communicator->subscribe('topic-name', function(array $message): void {
            logger()->info('Received message', $message);
            // Process your message
        });
    }
}

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use WZaradzki\MicroserviceCommunicator\MicroserviceCommunicationManager;

class ProcessMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(
        private readonly array $message
    ) {}

    public function handle(MicroserviceCommunicationManager $communicator): void
    {
        $communicator->publish('processed-messages', [
            'original_message' => $this->message,
            'processed_at' => now()->toIso8601String()
        ]);
    }
}

use Illuminate\Foundation\Events\Dispatchable;
use WZaradzki\MicroserviceCommunicator\MicroserviceCommunicationManager;

class MessageReceived
{
    use Dispatchable;

    public function __construct(
        public readonly array $message
    ) {}
}

// In your service provider
public function boot(): void
{
    Event::listen(function (MessageReceived $event) {
        // Process the message
    });
}

use Illuminate\Console\Command;
use WZaradzki\MicroserviceCommunicator\MicroserviceCommunicationManager;

class ConsumeMessages extends Command
{
    protected $signature = 'messages:consume {topic} {--driver=azure}';
    
    public function handle(MicroserviceCommunicationManager $communicator): void
    {
        $communicator->subscribe($this->argument('topic'), function(array $message): void {
            $this->info('Processing message: ' . json_encode($message));
            // Process message
        });
    }
}

use WZaradzki\MicroserviceCommunicator\MicroserviceCommunicationManager;

test('message is published successfully', function () {
    $manager = Mockery::mock(MicroserviceCommunicationManager::class);
    
    $manager->shouldReceive('publish')
        ->once()
        ->with('topic-name', ['key' => 'value'])
        ->andReturn(true);
        
    $this->app->instance(MicroserviceCommunicationManager::class, $manager);
    
    // Test your code
    expect(/* your test */)
        ->toBeTrue();
});

use WZaradzki\MicroserviceCommunicator\Exceptions\BrokerException;
use Illuminate\Support\Facades\Log;

try {
    $communicator->publish('topic', $message);
} catch (BrokerException $e) {
    Log::error('Failed to publish message', [
        'error' => $e->getMessage(),
        'topic' => 'topic',
        'message' => $message,
        'trace' => $e->getTrace()
    ]);
    
    report($e); // Uses Laravel 11's error reporting
}
bash
php artisan vendor:publish --provider="WZaradzki\MicroserviceCommunicator\MicroserviceCommunicationServiceProvider"