Coder Social home page Coder Social logo

laravel-centrifugo's Introduction

Documentation EN | RU

Laravel + Centrifugo

Centrifugo broadcast driver for Laravel 8.75.0 - 9

Build Status Latest Version Quality Score StyleCI Total Downloads Software License

Introduction

Centrifugo broadcaster for laravel , based on:

Features

  • Compatible with latest Centrifugo 3.2.0 ๐Ÿš€
  • Wrapper over Centrifugo HTTP API ๐Ÿ”Œ
  • Authentication with JWT token (HMAC algorithm) for anonymous, authenticated user and private channel ๐Ÿ—๏ธ

Requirements

  • PHP >= 7.4 , 8.0, 8.1
  • Laravel 8.75.0 - 10.0
  • guzzlehttp/guzzle 6 - 7
  • Centrifugo Server 3.2.0 or newer (see here)

Installation

Require this package with composer:

composer req denis660/laravel-centrifugo

Open your config/app.php and add the following to the providers array:

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

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

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

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

CENTRIFUGO_TOKEN_HMAC_SECRET_KEY=token_hmac_secret_key-from-centrifugo-config
CENTRIFUGO_API_KEY=api_key-from-centrifugo-config
CENTRIFUGO_URL=http://localhost:8000

These lines are optional:

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

Don't forget to change BROADCAST_DRIVER setting in .env file!

BROADCAST_DRIVER=centrifugo

Basic Usage

To configure Centrifugo server, read official documentation

For broadcasting events, see official documentation of laravel

A simple client usage example:

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

    }
}

Available methods

Name Description
publish(string $channel, array $data, $skipHistory = false) Send message into channel.
broadcast(array $channels, array $data, $skipHistory = false) Send message into multiple channel.
presence(string $channel) Get channel presence information (all clients currently subscribed on this channel).
presenceStats(string $channel) Get channel presence information in short form (number of clients).
history(string $channel, $limit = 0, $since = [], $reverse = false) Get channel history information (list of last messages sent into channel).
historyRemove(string $channel) Remove channel history information.
subscribe(string $channel, string $user, $client = '') subscribe user from channel.
unsubscribe(string $channel, string $user, string $client = '') Unsubscribe user from channel.
disconnect(string $user_id) Disconnect user by it's ID.
channels(string $pattern = '') Get channels information (list of currently active channels).
info() Get stats information about running server nodes.
generateConnectionToken(string $userId = '', int $exp = 0, array $info = [], array $channels = []) Generate connection token.
generatePrivateChannelToken(string $client, string $channel, int $exp = 0, array $info = []) Generate private channel token.

License

The MIT License (MIT). Please see [License File](https://github.com/denis660/laravel-centrifugo/blob/master/LICENSE for more information.

laravel-centrifugo's People

Contributors

davidoc26 avatar denis660 avatar laravel-shift avatar opekunov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

laravel-centrifugo's Issues

Laravel 11 support

I would like to request support for Laravel 11. As Laravel 11 brings new features and improvements, it would be beneficial for the community to have this project compatible with the latest version. Specifically, support for Laravel 11 would help us leverage the new functionalities and maintain up-to-date security standards.

Undefined variable $private

Hi,

Thank you for this package is very helpful while working with centrifugo.

After using it for a while sometimes we get this error.

Undefined variable $private {"exception":"[object] (ErrorException(code: 0): Undefined variable $private at /var/www/html/vendor/denis660/laravel-centrifugo/src/CentrifugoBroadcaster.php:62)"}

I will push a PR asap.

Centrifugo v3 compatibility

Hello! Currently readme states that this package:

Compatible with latest Centrifugo 2.8.5

Recently we released Centrifugo v3 with many improvements and new features. Unfortunately there are also several backwards incompatible changes. Everything described in Migrating to v3 guide.

Regarding this package I noticed the following things to be updated:

  • Response format of channels API method changed, and it now accepts pattern argument
  • Behavior of history API changed - it does not return publications by default, only current stream position, to return publications limit argument must be set to a value greater than 0.
  • Some additional fields added to publish, broadcast, history methods (though new params fields do not break requests, just extend functionality)
  • Also, some deprecated option names like secret now removed from Centrifugo, it's now token_hmac_secret_key - so maybe this package should match updated option names.

Would be awesome to have laravel-centrifugo inherit all of this. I am ready to help answering any questions during migration.

And thanks for an awesome package @denis660!

Undefined index: scheme, docker

I use docker and need to connect to url like "centrifugo:8001". In docker-compose i have service named 'centrifugo'. If provide CENTRIFUGO_URL=centrifugo then got "Undefined index: scheme" denis660/laravel-centrifugo/src/Centrifugo.php line 275. If provide CENTRIFUGO_URL=http://centrifugo then got cURL error 6: Could not resolve host: centrifugo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.