meetjet/laravel-centrifugo

这是我创建的包laravel-centrifugo

1.0.1 2021-10-15 13:30 UTC

This package is auto-updated.

Last update: 2024-09-07 22:11:26 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

特性

  • 兼容最新Centrifugo 3.0.3
  • 包含与Laravel Sail一起设置的说明和配置文件

要求

  • PHP >= 7.4
  • Laravel 7.30.4 - 8
  • Guzzle 6 - 7
  • Centrifugo服务器3.0.3或更高版本(见这里

安装

您可以通过Composer安装此包

composer require meetjet/laravel-centrifugo

打开您的config/broadcasting.php并添加新的连接,如下所示

'centrifugo' => [
    'driver' => 'centrifugo',
    'secret'  => env('CENTRIFUGO_SECRET'),
    'apikey'  => env('CENTRIFUGO_APIKEY'),
    'url'     => env('CENTRIFUGO_URL', 'https://: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)
],

此外,您还应在.env文件中添加这两行

CENTRIFUGO_SECRET=token_hmac_secret_key-from-centrifugo-config
CENTRIFUGO_APIKEY=api_key-from-centrifugo-config
CENTRIFUGO_URL=https://:8000

这些行是可选的

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

然后更改.env文件中的BROADCAST_DRIVER设置

BROADCAST_DRIVER=centrifugo

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Meetjet\LaravelCentrifugo\LaravelCentrifugoServiceProvider" --tag="laravel-centrifugo-config"

这是发布配置文件的内容

return [
];

使用Laravel Sail设置本地Centrifugo服务器

通过命令将centrifugo配置文件复制到项目根目录

php artisan centrifugo:setup

将Centrifugo配置块添加到docker-compose.yml文件的服务部分

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

打开您的.env并更改centrifugo api url

CENTRIFUGO_URL=http://centrifugo:8008

重启Laravel Sail。

使用

要配置Centrifugo服务器,请阅读官方文档

有关广播事件的详细信息,请参阅Laravel官方文档

与Centrifugo客户端一起使用

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

与Laravel广播功能一起使用

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

测试

composer test

变更日志

请参阅CHANGELOG以获取最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请审查我们的安全策略以了解如何报告安全漏洞。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。