MqttTopicBuilder 10.0.1
dotnet add package MqttTopicBuilder --version 10.0.1
NuGet\Install-Package MqttTopicBuilder -Version 10.0.1
<PackageReference Include="MqttTopicBuilder" Version="10.0.1" />
<PackageVersion Include="MqttTopicBuilder" Version="10.0.1" />
<PackageReference Include="MqttTopicBuilder" />
paket add MqttTopicBuilder --version 10.0.1
#r "nuget: MqttTopicBuilder, 10.0.1"
#:package MqttTopicBuilder@10.0.1
#addin nuget:?package=MqttTopicBuilder&version=10.0.1
#tool nuget:?package=MqttTopicBuilder&version=10.0.1
<h1 align="center"> <a href="https://www.nuget.org/packages/MqttTopicBuilder"> MqttTopicBuilder </a> </h1>
<p align="center"> <a href="https://www.nuget.org/packages/MqttTopicBuilder/"><img src="https://img.shields.io/nuget/v/MqttTopicBuilder.svg" alt="NuGet"></a> <a href="https://www.nuget.org/packages/MqttTopicBuilder/"><img src="https://img.shields.io/nuget/dt/MqttTopicBuilder.svg" alt="NuGet Downloads"></a> <a href="https://github.com/pBouillon/MqttTopicBuilder/blob/main/LICENSE"><img src="https://img.shields.io/github/license/pBouillon/MqttTopicBuilder.svg" alt="License"></a> </p>
<p align="center"> Build valid and verified MQTT topics </p>
Overview
MqttTopicBuilder helps you construct valid MQTT topics using a fluent builder API. It enforces MQTT topic rules at build time, preventing common mistakes like using wildcards in publisher topics or placing wildcards in invalid positions.
Installation
Install via .NET CLI:
dotnet add package MqttTopicBuilder
Or via Package Manager Console:
Install-Package MqttTopicBuilder
Quick Start
Building Topics
Use the TopicBuilder to construct topics with validation based on whether you're publishing or subscribing:
// Subscriber topic with wildcards
var subscribeTopic = new TopicBuilder(TopicConsumer.Subscriber)
.AddTopic("sensors")
.AddTopic("temperature")
.AddMultiLevelWildcard()
.Build();
Console.WriteLine(subscribeTopic);
// Output: "sensors/temperature/#"
// Publisher topic (wildcards not allowed)
var publishTopic = new TopicBuilder(TopicConsumer.Publisher)
.AddTopic("sensors")
.AddTopic("temperature")
.AddTopic("room1")
.Build();
Console.WriteLine(publishTopic);
// Output: "sensors/temperature/room1"
Topic Properties
Access topic metadata through the Topic object:
var topic = new TopicBuilder(TopicConsumer.Subscriber)
.AddTopic("home")
.AddTopic("livingroom")
.AddTopic("temperature")
.Build();
Console.WriteLine(topic.Value); // "home/livingroom/temperature"
Console.WriteLine(topic.Levels); // 3
Creating Topics from Strings
Convert existing topic strings into Topic objects:
var topic = Topic.FromString("home/kitchen/humidity");
// or using implicit conversion:
// var topic = (Topic)"home/kitchen/humidity";
Console.WriteLine(topic.Value); // "home/kitchen/humidity"
Console.WriteLine(topic.Levels); // 3
Validation
Validate topic strings before using them:
Topic integrity can also be checked sing the TopicValidator methods:
TopicValidator.ValidateTopic("a/wrong/#/Topic");
// Will throw an exception since no topic is allowed after '#'
"wrong+Topic".ValidateTopicForAppending();
// Will throw an exception since '+' is not allowed in a topic
MQTT Topics rules are checked, such as if the topic is not blank, is utf-8, does not contains any forbidden character, and so on.
If any rule isn't respected, this will throw a specific exception, inheriting from the
MqttBaseException.
Features
- Fluent API - Intuitive builder pattern for constructing topics
- Validation - Enforces MQTT specification rules
- Wildcard support - Proper handling of single-level (
+) and multi-level (#) wildcards - Publisher and Subscriber modes - Build topics with validation rules specific to each use case
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.