dapr / dapr/components-contrib

jetstream pubsub: how to subscribe to multiple subjects of one stream?

Open
#2,385 8 comments 0 reactions 1 assignee Claimed by @Jarema View on GitHub
help wanted pinned
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:
![image](https://user-images.githubusercontent.com/117846623/209085685-081de157-ae3c-43c7-be79-f40317c85551.png)

Dapr now creates a consumer for one of these subjects
![image](https://user-images.githubusercontent.com/117846623/209085941-6aa65178-a614-4143-9678-7b536a2d17cb.png)

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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.