PHPackages                             superbalist/laravel4-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. superbalist/laravel4-pubsub

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

superbalist/laravel4-pubsub
===========================

A Pub-Sub abstraction for Laravel 4

3.2.0(7y ago)125.5k↓48.7%1MITPHPPHP &gt;=5.6.0

Since Sep 15Pushed 3y ago6 watchersCompare

[ Source](https://github.com/Superbalist/laravel4-pubsub)[ Packagist](https://packagist.org/packages/superbalist/laravel4-pubsub)[ RSS](/packages/superbalist-laravel4-pubsub/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (9)Versions (17)Used By (1)

laravel4-pubsub
===============

[](#laravel4-pubsub)

A Pub-Sub abstraction for Laravel 4.

[![Author](https://camo.githubusercontent.com/abd4e3e2e71081ad01ef09a60c49d70c5e0677497f38918e740703cd02605078/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d40737570657262616c6973742d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/superbalist)[![Build Status](https://camo.githubusercontent.com/45b2b00021ebaec18acb717378b54de8f573b718baf517ea26dc879f95bfa3ee/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f537570657262616c6973742f6c61726176656c342d7075627375622f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Superbalist/laravel4-pubsub)[![StyleCI](https://camo.githubusercontent.com/75aacdbc2d968889013f68ec1fc0a93b78cb2c4bc6383b0d1b9dbf06dc2b8f42/68747470733a2f2f7374796c6563692e696f2f7265706f732f36373738313135352f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/67781155)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/e895481e5cf38842ac34b433d3db567d425b19d776200489637271306fde1c63/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737570657262616c6973742f6c61726176656c342d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/superbalist/laravel4-pubsub)[![Total Downloads](https://camo.githubusercontent.com/516149bf007a7744d1f2c2448ff0aa0e3ab7d93d1804bae1183922e95185cda7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f737570657262616c6973742f6c61726176656c342d7075627375622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/superbalist/laravel4-pubsub)

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

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

The following adapters are supported:

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

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

[](#installation)

```
composer require superbalist/laravel4-pubsub
```

The package has a default configuration built-in.

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

```
php artisan config:publish superbalist/laravel4-pubsub
```

You can then edit the generated config at `app/config/packages/superbalist/laravel4-pubsub/config.php`.

Register the service provider in app.php

```
'providers' => [
    // ...
    'Superbalist\Laravel4PubSub\PubSubServiceProvider'
]
```

Register the facade in app.php

```
'aliases' => [
    // ...
    'PubSub' => 'Superbalist\Laravel4PubSub\PubSubFacade',
]
```

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)

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 is a sample subscriber written as an artisan command.

```
