2017 © Pedro Peláez
 

library laravel4-pubsub

A Pub-Sub abstraction for Laravel 4

image

superbalist/laravel4-pubsub

A Pub-Sub abstraction for Laravel 4

  • Friday, July 27, 2018
  • by matthewgoslett
  • Repository
  • 23 Watchers
  • 1 Stars
  • 6,568 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 10 % Grown

The README.md

laravel4-pubsub

A Pub-Sub abstraction for Laravel 4., (*1)

Author Build Status StyleCI Software License Packagist Version Total Downloads, (*2)

This package is a wrapper bridging php-pubsub into Laravel 4., (*3)

For Laravel 5 support, use the package https://github.com/Superbalist/laravel-pubsub, (*4)

The following adapters are supported: * Local * /dev/null * Redis * Kafka (see separate installation instructions below) * Google Cloud * HTTP, (*5)

Installation

composer require superbalist/laravel4-pubsub

The package has a default configuration built-in., (*6)

To customize the configuration file, publish the package configuration using Artisan., (*7)

php artisan config:publish superbalist/laravel4-pubsub

You can then edit the generated config at app/config/packages/superbalist/laravel4-pubsub/config.php., (*8)

Register the service provider in app.php, (*9)

'providers' => [
    // ...
    'Superbalist\Laravel4PubSub\PubSubServiceProvider'
]

Register the facade in app.php, (*10)

'aliases' => [
    // ...
    'PubSub' => 'Superbalist\Laravel4PubSub\PubSubFacade',
]

Kafka Adapter Installation

Please note that whilst the package is bundled with support for the php-pubsub-kafka adapter, the adapter is not included by default., (*11)

This is because the KafkaPubSubAdapter has an external dependency on the librdkafka c library and the php-rdkafka PECL extension., (*12)

If you plan on using this adapter, you will need to install these dependencies by following these installation instructions., (*13)

You can then include the adapter using:, (*14)

composer require superbalist/php-pubsub-kafka

Usage

// get the pub-sub manager
$pubsub = app('pubsub');

// note: function calls on the manager are proxied through to the default connection
// eg: you can do this on the manager OR a connection
$pubsub->publish('channel_name', 'message');

// get the default connection
$pubsub = app('pubsub.connection');
// or
$pubsub = app(\Superbalist\PubSub\PubSubAdapterInterface::class);

// get a specific connection
$pubsub = app('pubsub')->connection('redis');

// publish a message
// the message can be a string, array, bool, object - anything which can be json encoded
$pubsub->publish('channel_name', 'this is where your message goes');
$pubsub->publish('channel_name', ['key' => 'value']);
$pubsub->publish('channel_name', true);

// publish multiple messages
$messages = [
    'message 1',
    'message 2',
];
$pubsub->publishBatch('channel_name', $messages);

// subscribe to a channel
$pubsub->subscribe('channel_name', function ($message) {
    var_dump($message);
});

// all the above commands can also be done using the facade
PubSub::connection('kafka')->publish('channel_name', 'Hello World!');

PubSub::connection('kafka')->subscribe('channel_name', function ($message) {
    var_dump($message);
});

Creating a Subscriber

A lot of pub-sub adapters will contain blocking subscribe() calls, so these commands are best run as daemons running as a supervisor process., (*15)

This is a sample subscriber written as an artisan command., (*16)

<?php

namespace App\Commands;

use App;
use Illuminate\Console\Command;

class MyExampleSubscriber extends Command
{
    /**
     * The name and signature of the subscriber command.
     *
     * @var string
     */
    protected $name = 'subscriber:name';

    /**
     * The subscriber description.
     *
     * @var string
     */
    protected $description = 'PubSub subscriber for ________';

    /**
     * Execute the console command.
     */
    public function fire()
    {
        $pubsub = App::make('pubsub');
        $pubsub->subscribe('channel_name', function ($message) {

        });
    }
}

Kafka Subscribers

For subscribers which use the php-pubsub-kafka adapter, you'll likely want to change the consumer_group_id per subscriber., (*17)

To do this, you need to use the PubSubConnectionFactory to create a new connection per subscriber. This is because the consumer_group_id cannot be changed once a connection is created., (*18)

Here is an example of how you can do this:, (*19)

<?php

namespace App\Commands;

use App;
use Config;
use Illuminate\Console\Command;

class MyExampleKafkaSubscriber extends Command
{
    /**
     * The name and signature of the subscriber command.
     *
     * @var string
     */
    protected $name = 'subscriber:name';

    /**
     * The subscriber description.
     *
     * @var string
     */
    protected $description = 'PubSub subscriber for ________';

    /**
     * Execute the console command.
     */
    public function fire()
    {
        $config = Config::get('laravel4-pubsub::connections.kafka');
        $config['consumer_group_id'] = self::class;
        $factory = App::make('pubsub.factory');
        $pubsub = $factory->make('kafka', $config);
        $pubsub->subscribe('channel_name', function ($message) {

        });
    }
}

Adding a Custom Driver

Please see the php-pubsub documentation Writing an Adapter., (*20)

To include your custom driver, you can call the extend() function., (*21)

$manager = app('pubsub');
$manager->extend('custom_connection_name', function ($config) {
    // your callable must return an instance of the PubSubAdapterInterface
    return new MyCustomPubSubDriver($config);
});

// get an instance of your custom connection
$pubsub = $manager->connection('custom_connection_name');

The Versions

27/07 2018

dev-version-bump-3.0.1

dev-version-bump-3.0.1

A Pub-Sub abstraction for Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Superbalist.com a division of Takealot Online (Pty) Ltd

25/07 2018
16/05 2017
03/01 2017

2.0.1

2.0.1.0

A Pub-Sub abstraction for Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Superbalist.com a division of Takealot Online (Pty) Ltd

05/10 2016

2.0.0

2.0.0.0

A Pub-Sub abstraction for Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Superbalist.com a division of Takealot Online (Pty) Ltd

15/09 2016

1.0.1

1.0.1.0

A Pub-Sub abstraction for Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Superbalist.com a division of Takealot Online (Pty) Ltd

15/09 2016

1.0.0

1.0.0.0

A Pub-Sub abstraction for Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Superbalist.com a division of Takealot Online (Pty) Ltd