meetjet/laravel-centrifugo

This is my package laravel-centrifugo

1.0.1 2021-10-15 13:30 UTC

This package is auto-updated.

Last update: 2024-11-07 22:36:03 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Features

  • Compatible with latest Centrifugo 3.0.3
  • Contains instructions and configuration file for setting up with Laravel Sail

Requirements

  • PHP >= 7.4
  • Laravel 7.30.4 - 8
  • Guzzle 6 - 7
  • Centrifugo Server 3.0.3 or newer (see here)

Installation

You can install the package via composer:

composer require meetjet/laravel-centrifugo

Open your config/broadcasting.php and add new connection like this:

'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 (require verify=true)
],

Also, you should add these two lines to your .env file:

CENTRIFUGO_SECRET=token_hmac_secret_key-from-centrifugo-config
CENTRIFUGO_APIKEY=api_key-from-centrifugo-config
CENTRIFUGO_URL=http://localhost:8000

These lines are optional:

CENTRIFUGO_SSL_KEY=/etc/ssl/some.pem
CENTRIFUGO_VERIFY=false

Then change BROADCAST_DRIVER setting in .env file:

BROADCAST_DRIVER=centrifugo

You can publish the config file with:

php artisan vendor:publish --provider="Meetjet\LaravelCentrifugo\LaravelCentrifugoServiceProvider" --tag="laravel-centrifugo-config"

This is the contents of the published config file:

return [
];

Setup local Centrifugo server with Laravel Sail

Copy centrifugo config file to project root folder via command:

php artisan centrifugo:setup

Add Centrifugo config block into services section of docker-compose.yml file:

centrifugo:
    image: centrifugo/centrifugo:latest
    volumes:
        - ./centrifugo.json:/centrifugo/centrifugo.json
    command: centrifugo -c centrifugo.json
    ports:
        - '8008:8008'
    networks:
        - sail
    ulimits:
        nofile:
            soft: 65535
            hard: 65535

Open your .env and change centrifugo api url:

CENTRIFUGO_URL=http://centrifugo:8008

Restart Laravel Sail.

Usage

To configure Centrifugo server, read official documentation

For broadcasting events, see official documentation of Laravel

Usage with Centrifugo Client

$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');

Usage with Laravel Broadcast feature

<?php

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');
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.