PHPackages                             milind/laravel-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. milind/laravel-pubsub

ActiveLibrary

milind/laravel-pubsub
=====================

A Pub-Sub abstraction for Laravel

065PHP

Since Jul 18Pushed 4y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#laravel-pubsub)

A Pub-Sub abstraction for Laravel.

[![Author](https://camo.githubusercontent.com/89d1829c61afc8a92333fe0e830ad71714d9fa0cb84cea315ba6162304ff688b/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406d696c696e642d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/milind)[![Build Status](https://camo.githubusercontent.com/e2daba308ba11f2c50b333a7285b9c7612ecd49d59b85ebf6842ab702e32da6e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d696c696e642f6c61726176656c2d7075627375622f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/milind/laravel-pubsub)[![StyleCI](https://camo.githubusercontent.com/88e3f0f60707f33088bd19bc217492ed6b048034360ca77510b22e17f63ac84f/68747470733a2f2f7374796c6563692e696f2f7265706f732f36373430353939332f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/67405993)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/59b9908c94184cdc9b1c2ab22e2851e4f4a1087ceafef95ebe1bd6599ae5a091/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d696c696e642f6c61726176656c2d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/milind/laravel-pubsub)[![Total Downloads](https://camo.githubusercontent.com/7097d30e27ae5b0821806bbffbb6d6e1c5235736a1e5d8e979b74d62cec12d3b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d696c696e642f6c61726176656c2d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/milind/laravel-pubsub)

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

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

Please note that **Laravel 5.3** is only supported up until version 2.0.2.

2.0.3+ supports **Laravel 5.4 and up** moving forward.

The following adapters are supported:

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

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

[](#installation)

```
composer require milind/laravel-pubsub
```

Register the service provider in app.php

```
'providers' => [
    // ...
    milind\LaravelPubSub\PubSubServiceProvider::class,
]
```

Register the facade in app.php

```
'aliases' => [
    // ...
    'PubSub' => milind\LaravelPubSub\PubSubFacade::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

HTTP_PUBSUB_URI=null
HTTP_PUBSUB_SUBSCRIBE_CONNECTION=redis

```

To customize the configuration file, publish the package configuration using Artisan.

```
php artisan vendor:publish --provider="milind\LaravelPubSub\PubSubServiceProvider"
```

You can then edit the generated 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/milind/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/milind/php-pubsub-kafka).

You can then include the adapter using:

```
composer require milind/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(\milind\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:

```
