daalvand/pubsub

A package for pub sub system

1.0.0 2021-09-24 10:04 UTC

This package is auto-updated.

Last update: 2024-04-24 15:33:27 UTC


README

  • This package used to publish and subscribe with Regis or Kafka

installation

install package

run composer require daalvand/pubsub

publish provider

Laravel

  • php artisan vendor:publish --provider="Daalvand\PubSub\PubSubServiceProvider"

Lumen

  • Add the service provider to bootstrap/app.php file:
<?php
//...
/** @var App $app */
$app->register(Daalvand\PubSub\PubSubServiceProvider::class);

Copy the config files from /vendor/daalvand/pubsub/src/config to config directory. Then configure it in /bootstrap/app.php file:

<?php
/** @var App $app */
$app->configure("pub-sub");

USAGE

Publisher

use Daalvand\PubSub\Facades\Publisher;

Publisher::publish('channel_name', $data);

Subscriber

use Daalvand\PubSub\Facades\Subscriber;
use Daalvand\PubSub\Message;

Subscriber::subscribe(['channel_one', 'channel_two'], function (Message $message) {
    switch ($message->getType()) {
    case 'channel_one':
        //a
        break;
    case 'channel_two':
        //b
        break;
    }
}, 'user');

NOTE

Notice that the real channel name is like below: {microservice_name}_{channel_name}_{environment}