MQTTnet Documentation

repository·master·Indexed 26 days ago

https://github.com/dotnet/mqttnet

A high-performance .NET library providing both an MQTT client and server (broker) supporting the MQTT protocol up to version 5. Features include ManagedMqttClient for automatic connection maintenance, support for TCP, TLS, and WebSockets, and compatibility with Microsoft Azure IoT Hub. Also includes MQTTNet.Extensions.TopicTemplate for safe MQTT topic management using moustache syntax.

Tokens
891
Snippets
2
Records
7
Agent score
40%

What's inside MQTTnet

  1. Overview of MQTTnet features

    master

    MQTTnet is a high-performance .NET library for MQTT-based communication supporting up to MQTT version 5. It provides both a client and a server (broker) implementation.

    Client Capabilities

    • Supports communication via TCP (+TLS) or WebSockets (WS).
    • LowLevelMqttClient: Provides low-level MQTT functionality.
    • ManagedMqttClient: Automatically maintains connections and subscriptions. It also queues and re-schedules application messages for higher QoS levels automatically.
    • Compatible with Microsoft Azure IoT Hub.

    Server (Broker) Capabilities

    • Supports multiple protocol versions simultaneously.
    • Extensible client credential validation.
    • Supports retained messages (requires implementing interface methods for persistence).
    • Supports WebSockets (via ASP.NET Core 2.0, available in a separate NuGet package).
    • Allows adding custom message interceptors to transform or extend received application messages.
    • Supports subscription validation to deny specific topics based on the requesting client.
  2. Use MQTTNet.Extensions.TopicTemplate for safe MQTT topic management

    master

    The MQTTNet.Extensions.TopicTemplate package provides logic for MQTT topic templating using moustache syntax (AsyncAPI dynamic channel address). It helps prevent errors like incorrect slash stripping or invalid topic filter characters when generating, parsing, or routing topics.

    Key capabilities include:

    • Safe handling of topic parameters.
    • Matching topics against templates and extracting parameter values.
    • Translating between templates for dynamic routing.
    • Deriving canonical topic filters from templates.
    • Integration with MqttApplicationMessageBuilder, MqttTopicFilterBuilder, and subscription builders.
  3. MQTTnet usage samples

    master

    The following sample implementations are recommended for getting started with MQTTnet:

    • Connect with a broker: Demonstrates establishing a connection.
    • Subscribing to data: Demonstrates how to listen for incoming messages.
    • Publishing data: Demonstrates how to send messages to topics.
    • Host own broker: Demonstrates how to run an MQTT server instance.
  4. Generate MQTT topic filters from templates

    master

    You can use MqttTopicTemplate with MqttTopicFilterBuilder to create a topic filter. When a template is used to build a filter, parameters are automatically replaced with the MQTT single-level wildcard (+).

    var template = new MqttTopicTemplate("App/v1/{sender}/message");
    
    var filter = new MqttTopicFilterBuilder()
        .WithTopicTemplate(template) // Parameters are replaced by '+' for filters
        .WithAtLeastOnceQoS()
        .WithNoLocal()
        .Build();
    
    // filter.Topic will be "App/v1/+/message"
  5. Generate MQTT application messages from templates

    master

    To send a message to a specific topic derived from a template, use WithParameter on the MqttTopicTemplate instance to replace placeholders with actual values. Then, use WithTopicTemplate on the MqttApplicationMessageBuilder.

    Note: If a parameter value contains a level separator (/) or MQTT topic filter characters, an ArgumentException will be thrown to ensure topic safety.