PHPackages                             lushdigital/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. lushdigital/lumen-pubsub

ActiveLibrary[Framework](/categories/framework)

lushdigital/lumen-pubsub
========================

A Pub-Sub abstraction for Lumen

0.1.3(7y ago)11.0k2MITPHPPHP &gt;=5.6.0

Since Mar 2Pushed 7y ago3 watchersCompare

[ Source](https://github.com/LUSHDigital/lumen-pubsub)[ Packagist](https://packagist.org/packages/lushdigital/lumen-pubsub)[ RSS](/packages/lushdigital-lumen-pubsub/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (5)Used By (0)

laravel-pubsub
==============

[](#laravel-pubsub)

A Pub-Sub abstraction for Laravel.

This package is a wrapper bridging [php-pubsub](https://github.com/Superbalist/php-pubsub) into Lumen.

The following adapters are supported:

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

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

[](#installation)

```
composer require lushdigital/lumen-pubsub
```

Register the service provider with Lumen in the `bootstrap/app.php` file:

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

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

```

To customize the configuration file simply copy it from `vendor/lushdigital/lumen-pubsub/config/pubsub.php` to `app/config/pubsub.php`. Then you need to add the following line to `bootstrap/app.php`:

```
$app->configure('pubsub');
```

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

Kafka Adapter Installation
--------------------------

[](#kafka-adapter-installation)

Please note that whilst the package is bundled with support for the [php-pubsub-kafka](https://github.com/Superbalist/php-pubsub-kafka)adapter, the adapter is not included by default.

This is because the KafkaPubSubAdapter has an external dependency on the `librdkafka c library` and the `php-rdkafka`PECL extension.

If you plan on using this adapter, you will need to install these dependencies by following these [installation instructions](https://github.com/Superbalist/php-pubsub-kafka).

You can then include the adapter using:

```
composer require superbalist/php-pubsub-kafka
```

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, json string, bool, object - anything which can be serialized
$pubsub->publish('channel_name', 'this is where your message goes');
$pubsub->publish('channel_name', ['key' => 'value']);
$pubsub->publish('channel_name', '{"key": "value"}');
$pubsub->publish('channel_name', true);

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

// all the aboce 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:

```
