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

ActiveLibrary

jezzdk/lumen-pubsub
===================

A Pub-Sub abstraction for Lumen

2.0.2(9y ago)012MITPHPPHP &gt;=5.6.0

Since Sep 6Pushed 9y ago1 watchersCompare

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

READMEChangelogDependencies (7)Versions (7)Used By (0)

lumen-pubsub
============

[](#lumen-pubsub)

A Pub-Sub abstraction for Laravel Lumen.

[![Author](https://camo.githubusercontent.com/abd4e3e2e71081ad01ef09a60c49d70c5e0677497f38918e740703cd02605078/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d40737570657262616c6973742d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/superbalist)[![Build Status](https://camo.githubusercontent.com/940c68eae5912849846cdd6074880dbcaf932771fd156a54823ad26b048e302a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f537570657262616c6973742f6c61726176656c2d7075627375622f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Superbalist/laravel-pubsub)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/857f78d00f75b55f51563df8cdc5032d171fc67f0569f5d8637bf2333f0c5f05/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737570657262616c6973742f6c61726176656c2d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/superbalist/laravel-pubsub)[![Total Downloads](https://camo.githubusercontent.com/3b11ab26ad56554e8f73908fe38223cef8e0a0bccd2b4c9055eb65e406bf7c3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f737570657262616c6973742f6c61726176656c2d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/superbalist/laravel-pubsub)

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

For **Laravel 4** support, use the package

The following adapters are supported:

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

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

[](#installation)

```
composer require jezzdk/lumen-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

```

To customize the configuration file, copy the package configuration into your project.

```
cp vendor/jezzdk/lumen-pubsub/config/pubsub.php config/pubsub.php
```

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

Register the service provider in bootstrap/app.php

```
$app->register(Superbalist\LaravelPubSub\PubSubServiceProvider::class);
```

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:

```
