LaraComponents / centrifuge-broadcaster by stenin-nikita

Centrifuge broadcaster for laravel >= 5.3
6,194
21
4
Package Data
Maintainer Username: stenin-nikita
Maintainer Contact: stenin.nikita@gmail.com (Nikita Stenin)
Package Create Date: 2016-12-26
Package Last Update: 2018-08-05
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-15 03:04:19
Package Statistics
Total Downloads: 6,194
Monthly Downloads: 22
Daily Downloads: 0
Total Stars: 21
Total Watchers: 4
Total Forks: 10
Total Open Issues: 2

Introduction

Centrifuge broadcaster for laravel >= 5.3

Requirements

  • PHP 5.6.4+ or newer
  • Laravel 5.3 or newer
  • Centrifugo Server 1.6.1 or newer (see here)

Installation

Require this package with composer:

composer require laracomponents/centrifuge-broadcaster

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

'providers' => [
    // ...
    LaraComponents\Centrifuge\CentrifugeServiceProvider::class,

    // And uncomment BroadcastServiceProvider
    App\Providers\BroadcastServiceProvider::class,
],

Open your config/broadcasting.php and add the following to it:

'connections' => [
    'centrifuge' => [
        'driver' => 'centrifuge',
        'secret' => env('CENTRIFUGE_SECRET'), // you secret key
        'url' => env('CENTRIFUGE_URL', 'http://localhost:8000'), // centrifuge api url
        'redis_api' => env('CENTRIFUGE_REDIS_API', false), // enable or disable Redis API
        'redis_connection' => env('CENTRIFUGE_REDIS_CONNECTION', 'default'), // name of redis connection
        'redis_prefix' => env('CENTRIFUGE_REDIS_PREFIX', 'centrifugo'), // prefix name for queue in Redis
        'redis_num_shards' => env('CENTRIFUGE_REDIS_NUM_SHARDS', 0), // number of shards for redis API queue
        'verify' => env('CENTRIFUGE_VERIFY', false), // Verify host ssl if centrifuge uses this
        'ssl_key' => env('CENTRIFUGE_SSL_KEY', null), // Self-Signed SSl Key for Host (require verify=true)
    ],
    // ...
],

For the redis configuration, add a new connection in config/database.php

'redis' => [
    'centrifuge' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),,
        'password' => env('REDIS_PASSWORD', null),
        'port' =>  env('REDIS_PORT', 6379),
        'database' => 0,
    ],
    // ...
],

You can also add a configuration to your .env file:

CENTRIFUGE_SECRET=very-long-secret-key
CENTRIFUGE_URL=http://localhost:8000
CENTRIFUGE_REDIS_API=false
CENTRIFUGE_REDIS_CONNECTION=centrifuge
CENTRIFUGE_REDIS_PREFIX=centrifugo
CENTRIFUGE_REDIS_NUM_SHARDS=0
CENTRIFUGE_SSL_KEY=/etc/ssl/some.pem
CENTRIFUGE_VERIFY=false

Do not forget to install the broadcast driver

BROADCAST_DRIVER=centrifuge

Basic Usage

To configure the Centrifugo server, read the official documentation

For broadcasting events, see the official documentation of laravel

A simple example of using the client:

<?php

namespace App\Http\Controllers;

use LaraComponents\Centrifuge\Centrifuge;

class ExampleController extends Controller
{
    public function home(Centrifuge $centrifuge)
    {
        // Send message into channel
        $centrifuge->publish('channel-name', [
            'key' => 'value'
        ]);

        // Generate token
        $token = $centrifuge->generateToken('user id', 'timestamp', 'info');

        // Generate api sign
        $apiSign = $centrifuge->generateApiSign('data');

        // ...
    }
}

Available methods

| Name | Description | |------|-------------| | publish(string $channel, array $data, string $client = null) | Send message into channel. | | broadcast(array $channels, array $data, string $client = null) | Send message into multiple channel. | | presence(string $channel) | Get channel presence information (all clients currently subscribed on this channel). | | history(string $channel) | Get channel history information (list of last messages sent into channel). | | unsubscribe(string $user_id, string $channel = null) | Unsubscribe user from channel. | | disconnect(string $user_id) | Disconnect user by its ID. | | channels() | Get channels information (list of currently active channels). | | stats() | Get stats information about running server nodes. | | generateToken(string $userOrClient, string $timestampOrChannel, string $info = "") | Generate token. | | generateApiSign(string $data) | Generate api sign. |

License

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