adaojunior / laravel-postgres-broadcast-driver by adaojunior

Postgresql broadcasting events driver for Laravel
1,076
7
1
Package Data
Maintainer Username: adaojunior
Maintainer Contact: itsjunnior@gmail.com (Adão Júnior)
Package Create Date: 2015-09-10
Package Last Update: 2015-10-07
Language: PHP
License: MIT
Last Refreshed: 2024-12-11 03:00:06
Package Statistics
Total Downloads: 1,076
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 7
Total Watchers: 1
Total Forks: 3
Total Open Issues: 0

Postgres Broadcasting Events Driver for Laravel

Installation

Using Composer:

composer require adaojunior/laravel-postgresql-broadcast-driver

In your config/app.php file add the following provider to your service providers array:

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

In your config/broadcasting.php file set the default driver to 'postgresql' and add the connection configuration like so:

'default' => 'postgresql',

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

Usage

Add a custom broadcast event to your application like so:

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];
    }
}

Now to publish in your application simply fire the event:

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

NodeJS Client (optional)

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