Giter Site home page Giter Site logo

centrifuge-broadcaster's Introduction

Attention! Maybe some diff with IRL.

  1. This package forked from "LaraComponents" to fit new Centrifugo v2.
  2. Drop Redis support (v2 don't support it), see official migration guide.
  3. Update generateToken(user id, timestamp, info) method (v2 uses only jwt auth workflow).

Introduction

Centrifuge broadcaster for laravel >= 5.7

Requirements

  • PHP 7.1.3+ or newer (tested on 7.2.10)
  • Laravel 5.3+ or newer (tested on 5.7.8)
  • Centrifugo Server 2 or newer (see here)

Installation

Require this package with composer:

composer require emprove/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',
        'url'          => env('CENTRIFUGE_URL', 'http://127.0.0.1:8000'),
        'token_ttl'    => env('CENTRIFUGE_TOKEN_TTL', 3600),
        'token_issuer' => env('APP_URL', 'default'),
        'secret'       => env('CENTRIFUGE_SECRET', null),
        'api_key'      => env('CENTRIFUGE_API_KEY', null),
        'ssl_key'      => env('CENTRIFUGE_SSL_KEY', null),
        'verify'       => env('CENTRIFUGE_VERIFY', false),
    ],
    // ...
],

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

CENTRIFUGE_API_KEY=very-long-secret-api-key
CENTRIFUGE_SECRET=very-long-secret-key
CENTRIFUGE_URL=http://localhost:8000
CENTRIFUGE_SSL_KEY=/etc/ssl/some.pem
CENTRIFUGE_TOKEN_TTL=3600 # seconds
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 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 $userId) Generate JWT token for client.
generateApiSign(string $data) Generate api sign.

License

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

centrifuge-broadcaster's People

Contributors

stenin-nikita avatar

Watchers

James Cloos avatar

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.