PHP code example of seddighi78 / laravel-nats

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

    

seddighi78 / laravel-nats example snippets


namespace App\Listeners;

use Seddighi78\LaravelNats\Events\MessageReceived;

class PrintOnNatsMessageReceivedListener
{
    public function handle(MessageReceived $event): void
    {
        echo $event->message; // you can proccess the messeage here
    }
}

use Seddighi78\LaravelNats\Events\MessageReceived;
use App\Listeners\PrintOnNatsMessageReceivedListener;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        MessageReceived::class => [
            PrintOnNatsMessageReceivedListener::class,
        ]
    ];
}

$client = app(\Seddighi78\LaravelNats\Factories\NatsClientFactoryInterface::class)->getClient();
$client->publish('main', 'test');

use Seddighi78\LaravelNats\Facades\Nats;

// calling publish method 
Nats::getClient()->publish('main', 'test');
bash
php artisan vendor:publish --provider="Seddighi78\LaravelNats\NatsServiceProvider" 
bash
php artisan nats:subscriber:work {subject}