denis660/laravel-centrifugo

Centrifugo broadcaster for laravel

5.0 2024-10-08 17:10 UTC

This package is auto-updated.

Last update: 2024-10-08 17:43:30 UTC


README

Documentation EN | RU

Laravel + Centrifugo

Centrifugo broadcast driver for Laravel 9 - 11

Build Status Latest Version Total Downloads Software License

Introduction

Centrifugo broadcaster for Laravel is based on:

Features

  • Compatibility with the latest version of Centrifugo v5.4.6 🚀
  • Wrapper for Centrifugo HTTP API 🔌
  • JWT token authentication (HMAC algorithm) for anonymous, authorized users, and private channels 🗝️

Requirements

  • PHP 8.0 - 8.3
  • Laravel 9 - 11
  • Guzzlehttp/Guzzle 6 - 7
  • Centrifugo Server v5 or newer (see here)

Installation

For Laravel 9-10:

composer require denis660/laravel-centrifugo

For Laravel 11, there are specific instructions below.

Выберите нужную вам версию

By default, broadcasting is disabled in new Laravel 11 applications. You can enable broadcasting using the install Artisan command:

php artisan install:broadcasting

If asked whether to install Reverb, answer “no.”

Then, install the package for working with Centrifugo via composer by running the following command:

composer require denis660/laravel-centrifugo

Configuration

Run the command centrifuge , which will install centrifuge-laravel with a reasonable set of default configuration options and generate default keys. If you want to make any changes to the configuration, you can update the environment variables in the .env file.

php artisan centrifuge:install

Credentials

To establish a connection with Centrifugo, you need to provide a set of Centrifugo credentials from the config.json file. These credentials are configured on the Centrifugo server, but Laravel will generate example keys that you should replace. You can specify these credentials with the following environment variables:

Required parameters:

CENTRIFUGO_TOKEN_HMAC_SECRET_KEY=token_hmac_secret_key-from-centrifugo-config
CENTRIFUGO_API_KEY=api_key-from-centrifugo-config

Optional parameters, modify if needed:

CENTRIFUGO_URL=http://localhost:8000
CENTRIFUGO_SSL_KEY=/etc/ssl/some.pem
CENTRIFUGO_VERIFY=false

Make sure to check the BROADCAST_DRIVER parameter in the .env file:

BROADCAST_DRIVER=centrifugo

Client SDKs

For working with clients, see the Client SDK API

Here is a list of SDKs supported by Centrifugal Labs:

  • JavaScript — for browser, NodeJS, and React Native
  • Golang — for Go language
  • Dart — for Dart and Flutter (mobile and web applications)
  • Swift — for native iOS development
  • Java — for native Android and general Java development
  • Python — real-time SDK for Python on top of asyncio

Basic Usage

Set up your Centrifugo server as detailed in the official documentation For sending events, refer to the official Laravel documentation

Here is a simple example of client usage:

<?php
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 a message to the news channel
        $centrifugo->publish('news', ['message' => 'Hello world']);

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

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

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

        // Get information about the news channel and its active clients
        $centrifugo->presence('news');
    }
}

Methods for generating client tokens

API Methods

License

MIT License. Please read the License File for more information.

Support the Project

USDT wallet: TUYJrA9VRtXhDFooESHyT8dQSyg5zmtUg7

Network: TRC20

Contributing 🤝