MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

Utility class supporting topic templates / dynamic topics

Open simonthum opened this issue 1 year ago • 1 comments

This is essentially some support code to handle dynamic topic subscription, publication and related functonality without falling back to String.Format() and other cumbersome handcrafted magic.

The code has seen a bit of production use and I figured it might be a useful addition to the library. I realize it could well live outside of MqttNet, but OTOH I've had several complaints that topic parsing/generation would be too cumbersome.

The MqttTopicTemplate class processes "topic templates" like A/B/{parameter}/D and provides a fluent interface to operate on the named parameter(s). A bunch of extension methods hook up with related MqttNet APIs, resulting in code like

var topicTemplate = new MqttTopicTemplate("A/v1/{param}/F");
var filter = topicTemplate.BuildFilter()
    .WithAtLeastOnceQoS()
    .WithNoLocal()
    .Build();  // subscribes A/v1/+/F

var msg = new MqttApplicationMessageBuilder()
    .WithTopicTemplate(
        topicTemplate.WithParameter("param", "foo"))
    .Build();  // A/v1/foo/F

Effectively, this enables one to handle subscription and publication from one topic template.

The syntax is simple and fairly common, inside and outside of pub/sub. It is not standardized, but AsyncAPI calls this a "channel address expression".

simonthum avatar Feb 17 '24 16:02 simonthum

@dotnet-policy-service agree [company="medDV GmbH'"]

simonthum avatar Feb 19 '24 08:02 simonthum

@dotnet-policy-service agree company="medDV GmbH"

simonthum avatar Feb 19 '24 08:02 simonthum

@chkr1011 It seems the CI failures require your attention.

simonthum avatar Feb 20 '24 06:02 simonthum

I like the idea of working with templates but in my opinion it should not be part of the core library. I would rather move it to an extension library like "MQTTnet.Extensions.TopicTemplates".

chkr1011 avatar Mar 02 '24 08:03 chkr1011

You mean alongside Rpc and Managed Client? That sounds fine.

simonthum avatar Mar 12 '24 08:03 simonthum

I'd need to know what can be done about the tests though.

simonthum avatar Mar 12 '24 08:03 simonthum

I have restructured the code for publication as an extension.

I would like to add more tests and examples, but first the build has to work. It seems that the failing security checks are unrelated. Is there anything more I can do but waiting?

simonthum avatar Mar 15 '24 15:03 simonthum

@simonthum I applied the code style from the team shared settings in Rider and also extended the release notes. If you agree with the changes I will merge the PR

chkr1011 avatar Apr 19 '24 17:04 chkr1011

Hi, sorry I did not use Rider. I am OK with the formatting changes, please go ahead and merge.

One minor thing: You also updated one file in the RPC extension which I did not change (at least not intentionally).

simonthum avatar Apr 21 '24 08:04 simonthum

I should add that unit tests have not yet been run on my side, so they might still contain glitches. I reworked them to be more instructive, but was unable to run them locally. I can probably fix things in a few days if required.

simonthum avatar Apr 21 '24 10:04 simonthum

Hi, thanks for merging!

I installed rider, got unit tests to work and go green 🟢

https://github.com/simonthum/MQTTnet/tree/fix-pr-1932

Should I make another PR or do you prefer to fetch them?

simonthum avatar Apr 23 '24 20:04 simonthum