PHP code example of meetjet / laravel-centrifugo

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

    

meetjet / laravel-centrifugo example snippets


'centrifugo' => [
    'driver' => 'centrifugo',
    'secret'  => env('CENTRIFUGO_SECRET'),
    'apikey'  => env('CENTRIFUGO_APIKEY'),
    'url'     => env('CENTRIFUGO_URL', 'http://localhost:8000'), // Centrifugo server api url
    'verify'  => env('CENTRIFUGO_VERIFY', false), // Verify host ssl if centrifugo uses this
    'ssl_key' => env('CENTRIFUGO_SSL_KEY', null), // Self-Signed SSl Key for Host (

return [
];

$centrifugo = new Meetjet\LaravelCentrifugo();

// Send message into channel
$centrifugo->publish('public', ['message' => 'Hello world']);

// Generate connection token
$token = $centrifugo->generateConnectionToken((string)Auth::id(), 0, [
    'name' => Auth::user()->name,
]);

// Generate private channel token
$apiSign = $centrifugo->generatePrivateChannelToken((string)Auth::id(), 'channel', time() + 5 * 60, [
    'name' => Auth::user()->name,
]);

//Get a list of currently active channels.
$centrifugo->channels();

//Get channel presence information (all clients currently subscribed on this channel).
$centrifugo->presence('public');



namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

class ServerCreated implements ShouldBroadcast
{
    use SerializesModels;

    /**
     * The user that created the server.
     *
     * @var \App\Models\User
     */
    public $user;

    /**
     * Create a new event instance.
     *
     * @param  \App\Models\User  $user
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('public');
    }
}
bash
php artisan vendor:publish --provider="Meetjet\LaravelCentrifugo\LaravelCentrifugoServiceProvider" --tag="laravel-centrifugo-config"
bash
php artisan centrifugo:setup