Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit ab8fcdc

Browse files
document note on changing consumer_group_id for kafka subscribers
1 parent f7921ad commit ab8fcdc

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,72 @@ class MyExampleSubscriber extends Command
174174
}
175175
```
176176

177+
### Kafka Subscribers ###
178+
179+
For subscribers which use the `php-pubsub-kafka` adapter, you'll likely want to change the `consumer_group_id` per
180+
subscriber.
181+
182+
To do this, you need to use the `PubSubConnectionFactory` to create a new connection per subscriber. This is because
183+
the `consumer_group_id` cannot be changed once a connection is created.
184+
185+
Here is an example of how you can do this:
186+
187+
```php
188+
<?php
189+
190+
namespace App\Console\Commands;
191+
192+
use Illuminate\Console\Command;
193+
use Superbalist\LaravelPubSub\PubSubConnectionFactory;
194+
use Superbalist\PubSub\PubSubAdapterInterface;
195+
196+
class MyExampleKafkaSubscriber extends Command
197+
{
198+
/**
199+
* The name and signature of the subscriber command.
200+
*
201+
* @var string
202+
*/
203+
protected $signature = 'subscriber:name';
204+
205+
/**
206+
* The subscriber description.
207+
*
208+
* @var string
209+
*/
210+
protected $description = 'PubSub subscriber for ________';
211+
212+
/**
213+
* @var PubSubAdapterInterface
214+
*/
215+
protected $pubsub;
216+
217+
/**
218+
* Create a new command instance.
219+
*
220+
* @param PubSubConnectionFactory $factory
221+
*/
222+
public function __construct(PubSubConnectionFactory $factory)
223+
{
224+
parent::__construct();
225+
226+
$config = config('pubsub.connections.kafka');
227+
$config['consumer_group_id'] = self::class;
228+
$this->pubsub = $factory->make('kafka', $config);
229+
}
230+
231+
/**
232+
* Execute the console command.
233+
*/
234+
public function handle()
235+
{
236+
$this->pubsub->subscribe('channel_name', function ($message) {
237+
238+
});
239+
}
240+
}
241+
```
242+
177243
## Adding a Custom Driver
178244

179245
Please see the [php-pubsub](https://github.com/Superbalist/php-pubsub) documentation **Writing an Adapter**.

0 commit comments

Comments
 (0)