PHPackages                             robier/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. robier/laravel-pubsub

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

robier/laravel-pubsub
=====================

A Pub-Sub abstraction for Laravel

5.0.0(5y ago)0110MITPHPPHP &gt;=7.2.0

Since Sep 6Pushed 3y agoCompare

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

READMEChangelogDependencies (8)Versions (25)Used By (0)

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

[](#laravel-pubsub)

A Pub-Sub abstraction for Laravel. *forked from Superbalist/laravel-pubsub*

[![Author](https://camo.githubusercontent.com/abd4e3e2e71081ad01ef09a60c49d70c5e0677497f38918e740703cd02605078/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d40737570657262616c6973742d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/superbalist)[![Build Status](https://camo.githubusercontent.com/eb303f5a7a9bb5934d4583ae588c1d61223e013e88146a07ca2da5307b11bb31/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f456e74616e65742f6c61726176656c2d7075627375622f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Entanet/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/57f3b77594cecd27520be0781df7718336b55221f74541f936abc0b0e756bc43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656e74616e65742f6c61726176656c2d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/entanet/laravel-pubsub)[![Total Downloads](https://camo.githubusercontent.com/23956d720352db9dfec784b52bd53ec657bb9d8d18f75e3db5f71222fc044bf1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656e74616e65742f6c61726176656c2d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/entanet/laravel-pubsub)

This package is a wrapper bridging [php-pubsub](https://github.com/Superbalist/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 entanet/laravel-pubsub
```

Register the service provider in app.php

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

Register the facade in app.php

```
'aliases' => [
    // ...
    'PubSub' => Superbalist\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
KAFKA_SECURITY_PROTOCOL=kafka-security-protocol
KAFKA_SASL_USERNAME=kafka-sasl-username
KAFKA_SASL_PASSWORD=kafks-sasl-password

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

```

If the KAFKA\_SECURITY\_PROTOCOL is set to either "SASL\_SSL" or "SASL\_PLAINTEXT", the credentials stored in KAFKA\_SASL\_USERNAME &amp; KAFKA\_SASL\_PASSWORD will be used to authenticate with the kafka server / cluster.

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

```
php artisan vendor:publish --provider="Superbalist\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/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, 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:

```
