$share subscriptions and C# client per subscription callback

$share/group/topic/item
topic/item

The $share topic doesn’t fire an event when configured via SubscribeOptionsBuilder.WithSubscription, but the non-$share one does.

The $share topic DOES fire a client.OnMessageReceived event.

Is it because MatchTopic() is trying to match “$share/group/topic/item” with “topic/item” because “topic/item” is what is sent from the broker?

Hi @tkuznia

For shared subscriptions in the .NET client, you can set up a subscription using the SubscribeOptionsBuilder with a per-subscription callback like this:

var builder = new SubscribeOptionsBuilder();var options = builder.WithSubscription(    new TopicFilter("$share/group/topic/item", QualityOfService.AtLeastOnce),    (sender, e) =>      {          Console.WriteLine($"Message received: {e.Message.PayloadAsString}");      })    .Build();

The shared subscription format should be $share/GROUPID/TOPIC where:

  • $share is the static shared subscription identifier
  • GROUPID is your group identifier
  • TOPIC is your actual topic subscription 3

There is a known issue reported where subscribing to both a regular topic and shared subscription of the same topic can cause unexpected behavior with message delivery

This may explain why you’re seeing the global OnMessageReceived event fire but not the per-subscription callback.

When using shared subscriptions, messages are distributed among subscribers in the same group rather than each subscriber receiving a copy

The broker sends the message with the actual topic (without the $share prefix), which is why you see “topic/item” in the message.

I hope this helps,
Best,
Dasha from The HiveMQ Team