MqttTopicBuilder 10.0.1

dotnet add package MqttTopicBuilder --version 10.0.1
                    
NuGet\Install-Package MqttTopicBuilder -Version 10.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MqttTopicBuilder" Version="10.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MqttTopicBuilder" Version="10.0.1" />
                    
Directory.Packages.props
<PackageReference Include="MqttTopicBuilder" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MqttTopicBuilder --version 10.0.1
                    
#r "nuget: MqttTopicBuilder, 10.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MqttTopicBuilder@10.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MqttTopicBuilder&version=10.0.1
                    
Install as a Cake Addin
#tool nuget:?package=MqttTopicBuilder&version=10.0.1
                    
Install as a Cake Tool

<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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.