PHP code example of kanata-php / conveyor-laravel-broadcaster

1. Go to this page and download the library: Download kanata-php/conveyor-laravel-broadcaster 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/ */

    

kanata-php / conveyor-laravel-broadcaster example snippets



return [
    // ...
    'providers' => [
        // ...
        Kanata\LaravelBroadcaster\ConveyorServiceProvider::class,
    ],
    // ...
];



return [
    // ...
    Kanata\LaravelBroadcaster\ConveyorServiceProvider::class,
];



return [
    // ...
    'conveyor' => [
        'driver' => 'conveyor',
        'protocol' => env('CONVEYOR_PROTOCOL', 'ws'),
        'host' => env('CONVEYOR_URI', 'localhost'),
        'port' => env('CONVEYOR_PORT', 8181),
    ],
];

> 
> 
> namespace App\Events;
> 
> use Illuminate\Broadcasting\InteractsWithBroadcasting;
> use Illuminate\Broadcasting\PrivateChannel;
> use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
> 
> class TestEvent implements ShouldBroadcastNow
> {
>     use InteractsWithBroadcasting;
> 
>     public function __construct(
>         public string $message,
>         public string $channel,
>     ) {
>         $this->broadcastVia('conveyor');
>     }
> 
>     public function broadcastOn(): array
>     {
>         return [
>             new PrivateChannel($this->channel),
>         ];
>     }
> }
> 

> event(new App\Events\TestEvent( message: 'my message', channel: 'my-channel'));
> 

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;

Route::get('/ws-client', function () {
    Auth::loginUsingId(1); // here we authorize for the sake of the example.

    return view('ws-client', [
        'protocol' => config('broadcasting.connections.conveyor.protocol'),
        'uri' => config('broadcasting.connections.conveyor.host'),
        'wsPort' => config('broadcasting.connections.conveyor.port'),
        'channel' => 'private-my-channel',
    ]);
});


// file: server.php

er;
use Conveyor\Events\MessageReceivedEvent;
use Conveyor\Events\PreServerStartEvent;

(new ConveyorServer())
    // if you want to see messages in the console 
    ->eventListeners([
        Conveyor\Constants::WEBSOCKET_SERVER_TOKEN => 'my-secure-conveyor-token',
        Conveyor\Constants::EVENT_MESSAGE_RECEIVED => function (MessageReceivedEvent $event) {
            var_dump($event->data);
        },
    ])
    ->port(8181)
    ->start();
bash
php artisan vendor:publish --provider="Kanata\LaravelBroadcaster\ConveyorServiceProvider"