PHP code example of zipzoft / laravel-message-transporter

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

    

zipzoft / laravel-message-transporter example snippets


// /config/message-transporter.php

return [
    'default' => env('SERVICE_BROADCASTER_DRIVER', 'none'),
    'connection_prefix' => 'app-services_',
    'queue' => true,
]

// /config/database.php

return [
    // ...
    
    'redis' => [
        // ....
        
        'app-services_producer' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => 2,
            'options' => [
                'prefix' => env('SERVICE_BROADCASTER_PREFIX'),
            ]
        ]
    ]
];



namespace App\Events;

use App\User;
use Zipzoft\MessageTransporter\ShouldBroadcastAppServices;
use Zipzoft\MessageTransporter\ShouldBroadcastAppServicesNow;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;

class UserCreated implements ShouldBroadcastAppServices
{
    // ....
    
    public $user;
    
    public function __construct(User $user)
    {
        $this->user = $user;
    }
    
    public function broadcastOn()
    {
        return [
            new Channel("app"),
            new PrivateChannel("call-center"),
        ];
    }
    
    
    // Supported method
    // broadcastWith, broadcastWhen, broadcastAs
}

use Zipzoft\MessageTransporter\ShouldBroadcastAppServicesNow;

// /config/database.php

return [
    // ...
    
    'redis' => [
        // ....
        
        'app-services_consumer' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => 2,
            'options' => [
                'prefix' => '',
            ]
        ]
    ]
];



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Zipzoft\MessageTransporter\Broadcasters\ServiceBroadcaster;
use Zipzoft\MessageTransporter\Event\OnMessage;

class SubscribeAppServicesCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'service:subscribe';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Subscribe other services';

    /**
     * @var ServiceBroadcaster 
     */
    private ServiceBroadcaster $broadcaster;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(ServiceBroadcaster $broadcaster)
    {
        parent::__construct();
        
        $this->broadcaster = $broadcaster;
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->broadcaster->subscribe(['myweb*'], function (OnMessage $event) {
            // $event->event (string|null)
            // $event->data (array|string|null)
            // $event->channel (string)
        });

        return 0;
    }
}
json
{
  "php": "^7.4|^8.0"
}

php artisan vendor:publish --class="Zipzoft\MessageTransporter\MessageTransporterServiceProvider" --tag="config"