dapr / dapr/components-contrib
jetstream pubsub: how to subscribe to multiple subjects of one stream?
- Dominant language
- Go
- Stars
- 602
- Forks
- 580
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 6
Description
How can I subscribe to multiple subjects that are in the same stream.
I have a stream that contains 3 subjects.
```
Information for Stream daprtest created 2022-12-02 18:01:18
Subjects: d.1, d.2, d.3
Replicas: 1
Storage: File
```
I want to have a subscriber for subject d.1 and d.2
```
[Topic("pubsub", "d.1")]
[HttpPost("/d1")]
public async Task OnTestTopic1( Object o )
{
return Ok();
}
[Topic("pubsub", "d.2")]
[HttpPost("/d2")]
public async Task OnTestTopic2( Object o )
{
return Ok();
}
```
this is the configuration:

Dapr now creates a consumer for one of these subjects

This consumer is random. I tried multiple times, sometimes a consumer for d.1 is created and sometimes a consumer for d.2
dapr logs this error:
```
time="2022-12-22T07:49:13.861826617Z" level=debug msg="nats: subscribed to subject d.2" app_id=bizerba.saas.cloud.provisioning.api instance=488b1af8c100 scope=dapr.contrib type=log ver=1.9.4
time="2022-12-22T07:49:13.962883843Z" level=error msg="error occurred while beginning pubsub for component failed to subscribe to topic d.2: nats: subject does not match consumer: %!s(MISSING)" app_id=bizerba.saas.cloud.provisioning.api instance=488b1af8c100 scope=dapr.runtime type=log ver=1.9.4
```
There is a workaround to solve this problem. If I create a pubsub component for each subscription with a different durable name, then I can subscribe to both subjects.
My question is, is there an easier way to do this? For example by defining the durable name on the REST endpoint by doing something like this:
```
[Topic("pubsub", "d.1", "myDurableName")]
[HttpPost("/d1")]
public async Task OnTestTopic1( Object o )
{
return Ok();
}
```
Contributor guide
Assessment
This issue has not been assessed yet.