PHP code example of alexusmai / laravel-centrifugo

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

    

alexusmai / laravel-centrifugo example snippets


'providers' => [
    // And uncomment BroadcastServiceProvider
    App\Providers\BroadcastServiceProvider::class,
],

        'centrifugo' => [
            'driver' => 'centrifugo',
            'token_hmac_secret_key' => env('CENTRIFUGO_TOKEN_HMAC_SECRET_KEY',''),
            'api_key'               => env('CENTRIFUGO_API_KEY',''),
            'url'                   => env('CENTRIFUGO_URL', 'http://localhost:8000'), // centrifugo 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 (


declare(strict_types = 1);

namespace App\Http\Controllers;


use denis660\Centrifugo\Centrifugo;
use Illuminate\Support\Facades\Auth;

class ExampleController
{

    public function example(Centrifugo $centrifugo)
    {
        // Send message into channel
        $centrifugo->publish('news', ['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('news');

    }
}