PHPackages                             averinuveren2/lumen-pubsub - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. averinuveren2/lumen-pubsub

ActiveLibrary[Framework](/categories/framework)

averinuveren2/lumen-pubsub
==========================

A Pub-Sub abstraction for Lumen

3.2.1(6y ago)1116MITPHPPHP ^7.2

Since Sep 6Pushed 6y agoCompare

[ Source](https://github.com/averinuveren2/lumen-pubsub)[ Packagist](https://packagist.org/packages/averinuveren2/lumen-pubsub)[ RSS](/packages/averinuveren2-lumen-pubsub/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (10)Versions (15)Used By (0)

Lumen-pubsub
============

[](#lumen-pubsub)

A Pub-Sub abstraction for lumen.

The following adapters are supported:

- Local
- /dev/null
- Redis
- Kafka (see separate installation instructions below)
- Google Cloud
- HTTP

Installation
------------

[](#installation)

```
composer require averinuveren2/lumen-pubsub
```

Register the service provider in app.php

```
$app->register(Averinuveren\LumenPubSub\PubSubServiceProvider::class);
```

Register the facade in app.php

```
$app->withFacades(true, [
    Averinuveren\LumenPubSub\PubSubFacade::class => 'PubSub'
]);
```

The package has a default configuration which uses the following environment variables.

```
PUBSUB_CONNECTION=redis

REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379

KAFKA_BROKERS=localhost

GOOGLE_CLOUD_PROJECT_ID=your-project-id-here
GOOGLE_CLOUD_KEY_FILE=path/to/your/gcloud-key.json

HTTP_PUBSUB_URI=null
HTTP_PUBSUB_SUBSCRIBE_CONNECTION=redis

```

You can then edit the generated config at `app/config/pubsub.php`.

Kafka Adapter Requirements
--------------------------

[](#kafka-adapter-requirements)

1. Install [librdkafka c library](https://github.com/edenhill/librdkafka)

    ```
    $ cd /tmp
    $ mkdir librdkafka
    $ cd librdkafka
    $ git clone https://github.com/edenhill/librdkafka.git .
    $ ./configure
    $ make
    $ make install
    ```
2. Install the [php-rdkafka](https://github.com/arnaud-lb/php-rdkafka) PECL extension

    ```
    $ pecl install rdkafka
    ```
3. Add the following to your php.ini file to enable the php-rdkafka extension `extension=rdkafka.so`

Usage
-----

[](#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
---------------------

[](#creating-a-subscriber)

The package includes a helper command `php artisan make:subscriber MyExampleSubscriber` to stub new subscriber command classes.

A lot of pub-sub adapters will contain blocking `subscribe()` calls, so these commands are best run as daemons running as a [supervisor](http://supervisord.org) process.

This generator command will create the file `app/Console/Commands/MyExampleSubscriber.php` which will contain:

```
