Code Monkey home page Code Monkey logo

laravel-centrifugo's Introduction

Laravel Centrifugo

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.

laravel-centrifugo's People

Contributors

zorca 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.