{"id":15188285,"url":"https://github.com/superbalist/laravel-pubsub","last_synced_at":"2025-10-02T03:30:31.954Z","repository":{"id":10898010,"uuid":"67405993","full_name":"Superbalist/laravel-pubsub","owner":"Superbalist","description":"A Pub-Sub abstraction for Laravel","archived":true,"fork":false,"pushed_at":"2022-07-18T11:33:33.000Z","size":47,"stargazers_count":68,"open_issues_count":8,"forks_count":42,"subscribers_count":38,"default_branch":"master","last_synced_at":"2025-01-16T17:50:42.249Z","etag":null,"topics":["laravel","laravel-pubsub","laravel5","laravel5-package","php","php-pubsub","pubsub","superbalist"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Superbalist.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2016-09-05T09:12:11.000Z","updated_at":"2024-07-19T07:13:03.000Z","dependencies_parsed_at":"2022-07-24T20:32:21.764Z","dependency_job_id":null,"html_url":"https://github.com/Superbalist/laravel-pubsub","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Superbalist%2Flaravel-pubsub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Superbalist%2Flaravel-pubsub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Superbalist%2Flaravel-pubsub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Superbalist%2Flaravel-pubsub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Superbalist","download_url":"https://codeload.github.com/Superbalist/laravel-pubsub/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234929293,"owners_count":18908907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","laravel-pubsub","laravel5","laravel5-package","php","php-pubsub","pubsub","superbalist"],"created_at":"2024-09-27T19:02:16.313Z","updated_at":"2025-10-02T03:30:26.698Z","avatar_url":"https://github.com/Superbalist.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# laravel-pubsub\n\nA Pub-Sub abstraction for Laravel.\n\n[![Author](http://img.shields.io/badge/author-@superbalist-blue.svg?style=flat-square)](https://twitter.com/superbalist)\n[![Build Status](https://img.shields.io/travis/Superbalist/laravel-pubsub/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/laravel-pubsub)\n[![StyleCI](https://styleci.io/repos/67405993/shield?branch=master)](https://styleci.io/repos/67405993)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n[![Packagist Version](https://img.shields.io/packagist/v/superbalist/laravel-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-pubsub)\n[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/laravel-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-pubsub)\n\nThis package is a wrapper bridging [php-pubsub](https://github.com/Superbalist/php-pubsub) into Laravel.\n\nFor **Laravel 4** support, use the package https://github.com/Superbalist/laravel4-pubsub\n\nPlease note that **Laravel 5.3** is only supported up until version 2.0.2.\n\n2.0.3+ supports **Laravel 5.4 and up** moving forward.\n\nThe following adapters are supported:\n* Local\n* /dev/null\n* Redis\n* Kafka (see separate installation instructions below)\n* Google Cloud\n* HTTP\n\n## Installation\n\n```bash\ncomposer require superbalist/laravel-pubsub\n```\n\nRegister the service provider in app.php\n```php\n'providers' =\u003e [\n    // ...\n    Superbalist\\LaravelPubSub\\PubSubServiceProvider::class,\n]\n```\n\nRegister the facade in app.php\n```php\n'aliases' =\u003e [\n    // ...\n    'PubSub' =\u003e Superbalist\\LaravelPubSub\\PubSubFacade::class,\n]\n```\n\nThe package has a default configuration which uses the following environment variables.\n```\nPUBSUB_CONNECTION=redis\n\nREDIS_HOST=localhost\nREDIS_PASSWORD=null\nREDIS_PORT=6379\n\nKAFKA_BROKERS=localhost\n\nGOOGLE_CLOUD_PROJECT_ID=your-project-id-here\nGOOGLE_CLOUD_KEY_FILE=path/to/your/gcloud-key.json\n\nHTTP_PUBSUB_URI=null\nHTTP_PUBSUB_SUBSCRIBE_CONNECTION=redis\n```\n\nTo customize the configuration file, publish the package configuration using Artisan.\n```bash\nphp artisan vendor:publish --provider=\"Superbalist\\LaravelPubSub\\PubSubServiceProvider\"\n```\n\nYou can then edit the generated config at `app/config/pubsub.php`.\n\n## Kafka Adapter Installation\n\nPlease note that whilst the package is bundled with support for the [php-pubsub-kafka](https://github.com/Superbalist/php-pubsub-kafka)\nadapter, the adapter is not included by default.\n\nThis is because the KafkaPubSubAdapter has an external dependency on the `librdkafka c library` and the `php-rdkafka`\nPECL extension.\n\nIf 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).\n\nYou can then include the adapter using:\n```bash\ncomposer require superbalist/php-pubsub-kafka\n```\n\n## Usage\n\n```php\n// get the pub-sub manager\n$pubsub = app('pubsub');\n\n// note: function calls on the manager are proxied through to the default connection\n// eg: you can do this on the manager OR a connection\n$pubsub-\u003epublish('channel_name', 'message');\n\n// get the default connection\n$pubsub = app('pubsub.connection');\n// or\n$pubsub = app(\\Superbalist\\PubSub\\PubSubAdapterInterface::class);\n\n// get a specific connection\n$pubsub = app('pubsub')-\u003econnection('redis');\n\n// publish a message\n// the message can be a string, array, bool, object - anything which can be json encoded\n$pubsub-\u003epublish('channel_name', 'this is where your message goes');\n$pubsub-\u003epublish('channel_name', ['key' =\u003e 'value']);\n$pubsub-\u003epublish('channel_name', true);\n\n// publish multiple messages\n$messages = [\n    'message 1',\n    'message 2',\n];\n$pubsub-\u003epublishBatch('channel_name', $messages);\n\n// subscribe to a channel\n$pubsub-\u003esubscribe('channel_name', function ($message) {\n    var_dump($message);\n});\n\n// all the above commands can also be done using the facade\nPubSub::connection('kafka')-\u003epublish('channel_name', 'Hello World!');\n\nPubSub::connection('kafka')-\u003esubscribe('channel_name', function ($message) {\n    var_dump($message);\n});\n```\n\n## Creating a Subscriber\n\nThe package includes a helper command `php artisan make:subscriber MyExampleSubscriber` to stub new subscriber command classes.\n\nA lot of pub-sub adapters will contain blocking `subscribe()` calls, so these commands are best run as daemons running\nas a [supervisor](http://supervisord.org) process.\n\nThis generator command will create the file `app/Console/Commands/MyExampleSubscriber.php` which will contain:\n```php\n\u003c?php\n\nnamespace App\\Console\\Commands;\n\nuse Illuminate\\Console\\Command;\nuse Superbalist\\PubSub\\PubSubAdapterInterface;\n\nclass MyExampleSubscriber extends Command\n{\n    /**\n     * The name and signature of the subscriber command.\n     *\n     * @var string\n     */\n    protected $signature = 'subscriber:name';\n\n    /**\n     * The subscriber description.\n     *\n     * @var string\n     */\n    protected $description = 'PubSub subscriber for ________';\n\n    /**\n     * @var PubSubAdapterInterface\n     */\n    protected $pubsub;\n\n    /**\n     * Create a new command instance.\n     *\n     * @param PubSubAdapterInterface $pubsub\n     */\n    public function __construct(PubSubAdapterInterface $pubsub)\n    {\n        parent::__construct();\n\n        $this-\u003epubsub = $pubsub;\n    }\n\n    /**\n     * Execute the console command.\n     */\n    public function handle()\n    {\n        $this-\u003epubsub-\u003esubscribe('channel_name', function ($message) {\n\n        });\n    }\n}\n```\n\n### Kafka Subscribers ###\n\nFor subscribers which use the `php-pubsub-kafka` adapter, you'll likely want to change the `consumer_group_id` per\nsubscriber.\n\nTo do this, you need to use the `PubSubConnectionFactory` to create a new connection per subscriber.  This is because\nthe `consumer_group_id` cannot be changed once a connection is created.\n\nHere is an example of how you can do this:\n\n```php\n\u003c?php\n\nnamespace App\\Console\\Commands;\n\nuse Illuminate\\Console\\Command;\nuse Superbalist\\LaravelPubSub\\PubSubConnectionFactory;\nuse Superbalist\\PubSub\\PubSubAdapterInterface;\n\nclass MyExampleKafkaSubscriber extends Command\n{\n    /**\n     * The name and signature of the subscriber command.\n     *\n     * @var string\n     */\n    protected $signature = 'subscriber:name';\n\n    /**\n     * The subscriber description.\n     *\n     * @var string\n     */\n    protected $description = 'PubSub subscriber for ________';\n\n    /**\n     * @var PubSubAdapterInterface\n     */\n    protected $pubsub;\n\n    /**\n     * Create a new command instance.\n     *\n     * @param PubSubConnectionFactory $factory\n     */\n    public function __construct(PubSubConnectionFactory $factory)\n    {\n        parent::__construct();\n\n        $config = config('pubsub.connections.kafka');\n        $config['consumer_group_id'] = self::class;\n        $this-\u003epubsub = $factory-\u003emake('kafka', $config);\n    }\n\n    /**\n     * Execute the console command.\n     */\n    public function handle()\n    {\n        $this-\u003epubsub-\u003esubscribe('channel_name', function ($message) {\n\n        });\n    }\n}\n```\n\n## Adding a Custom Driver\n\nPlease see the [php-pubsub](https://github.com/Superbalist/php-pubsub) documentation  **Writing an Adapter**.\n\nTo include your custom driver, you can call the `extend()` function.\n\n```php\n$manager = app('pubsub');\n$manager-\u003eextend('custom_connection_name', function ($config) {\n    // your callable must return an instance of the PubSubAdapterInterface\n    return new MyCustomPubSubDriver($config);\n});\n\n// get an instance of your custom connection\n$pubsub = $manager-\u003econnection('custom_connection_name');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperbalist%2Flaravel-pubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuperbalist%2Flaravel-pubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperbalist%2Flaravel-pubsub/lists"}