adaojunior/laravel-postgresql-broadcast-driver

Laravel 的 Postgresql 广播事件驱动程序

v1.0 2015-09-11 00:39 UTC

This package is auto-updated.

Last update: 2024-09-22 09:54:04 UTC


README

安装

使用 Composer

composer require adaojunior/laravel-postgresql-broadcast-driver

在你的 config/app.php 文件中,将以下提供者添加到你的服务提供者数组中

'providers' => [
    ...
    Adaojunior\PostgreSqlBroadcastDriver\BroadcastServiceProvider::class,
    ...
]

在你的 config/broadcasting.php 文件中将默认驱动设置为 'postgresql' 并添加连接配置,如下所示

'default' => 'postgresql',

'connections' => [
    ...
    'postgresql' => [
            'driver' => 'postgresql',
            'connection' => env('BROADCAST_PG_DB','pgsql')
        ]
    ...
]

用法

将自定义广播事件添加到你的应用程序中,如下所示

namespace App\Events;

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class Message extends Event implements ShouldBroadcast
{
    protected $message;

    public function __construct($message)
    {
        $this->message= $message;
    }

    public function broadcastOn()
    {
        return ['MessageChannel'];
    }

    public function broadcastWith()
    {
        return ['message' => $this->message];
    }
}

现在,在你的应用程序中发布事件,只需触发该事件

event(new App\Events\Message('Test publish!!!'));

NodeJS 客户端(可选)

npm install pg-pubsub --save
// server.js

var PGPubsub = require('pg-pubsub');

var instance = new PGPubsub('postgres://homestead:secret@localhost/homestead';

instance.addChannel('MessageChannel', function (payload) {
    console.log(payload);
});