MQTT-hnlyf 1.0.0

dotnet add package MQTT-hnlyf --version 1.0.0
                    
NuGet\Install-Package MQTT-hnlyf -Version 1.0.0
                    
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="MQTT-hnlyf" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MQTT-hnlyf" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MQTT-hnlyf" />
                    
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 MQTT-hnlyf --version 1.0.0
                    
#r "nuget: MQTT-hnlyf, 1.0.0"
                    
#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 MQTT-hnlyf@1.0.0
                    
#: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=MQTT-hnlyf&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MQTT-hnlyf&version=1.0.0
                    
Install as a Cake Tool

System.Net.MQTT

A high-performance, lightweight MQTT client and broker library for .NET.

Features

  • Multi-target support: .NET 6.0, .NET 8.0, .NET 10.0
  • MQTT 3.1.1 compliant: Full protocol support
  • High performance: Async/await, zero unnecessary allocations
  • TLS/SSL support: Secure connections with certificate support
  • QoS levels: At most once (0), At least once (1), Exactly once (2)
  • Retained messages: Message persistence support
  • Will messages: Last Will and Testament (LWT) support
  • Topic wildcards: Single-level (+) and multi-level (#) wildcards
  • Authentication: Pluggable authentication and authorization
  • Auto-reconnect: Automatic reconnection on disconnect

Solution Structure

MQTT/
├── MQTT.slnx                          # Solution file (slnx format)
├── README.md                          # This file
├── src/
│   ├── System.Net.MQTT/              # Client library
│   │   ├── IMqttClient.cs            # Client interface
│   │   ├── MqttClient.cs             # Client implementation
│   │   ├── MqttClientOptions.cs      # Connection options
│   │   ├── MqttApplicationMessage.cs # Message model
│   │   ├── MqttQualityOfService.cs   # QoS enum
│   │   ├── MqttProtocolVersion.cs    # Protocol versions
│   │   ├── MqttPacketType.cs         # Packet types
│   │   ├── MqttConnectResult.cs      # Connection result
│   │   ├── MqttSubscription.cs       # Subscription models
│   │   ├── MqttEventArgs.cs          # Event arguments
│   │   └── MqttExceptions.cs         # Exception types
│   │
│   └── System.Net.MQTT.Broker/       # Broker library
│       ├── MqttBroker.cs             # Broker implementation
│       ├── MqttBrokerOptions.cs      # Broker options
│       ├── MqttClientSession.cs      # Client session
│       ├── MqttAuthentication.cs     # Auth interfaces
│       └── MqttBrokerEventArgs.cs    # Broker events
│
└── samples/
    ├── MqttClient.Sample/            # Client sample
    └── MqttBroker.Sample/            # Broker sample

Quick Start

Install

# Client library
dotnet add package System.Net.MQTT

# Broker library
dotnet add package System.Net.MQTT.Broker

Client Usage

using System.Net.MQTT;

// Create client
var options = new MqttClientOptions
{
    Host = "localhost",
    Port = 1883,
    ClientId = "my-client",
    CleanSession = true
};

using var client = new MqttClient(options);

// Event handlers
client.MessageReceived += (s, e) =>
{
    Console.WriteLine($"Received: {e.Message.Topic} = {e.Message.PayloadAsString}");
};

// Connect
await client.ConnectAsync();

// Subscribe
await client.SubscribeAsync("sensors/#", MqttQualityOfService.AtLeastOnce);

// Publish
await client.PublishAsync("sensors/temperature", "25.5");

// Disconnect
await client.DisconnectAsync();

Broker Usage

using System.Net.MQTT.Broker;

// Create broker
var options = new MqttBrokerOptions
{
    Port = 1883,
    AllowAnonymous = true
};

using var broker = new MqttBroker(options);

// Event handlers
broker.ClientConnected += (s, e) =>
{
    Console.WriteLine($"Client connected: {e.Session.ClientId}");
};

broker.MessagePublished += (s, e) =>
{
    Console.WriteLine($"Message: {e.Message.Topic} = {e.Message.PayloadAsString}");
};

// Start broker
await broker.StartAsync();

// Wait for shutdown signal
await Task.Delay(Timeout.Infinite);

// Stop broker
await broker.StopAsync();

Client Options

Option Type Default Description
Host string - Broker host address
Port int 1883 Broker port
ClientId string GUID Client identifier
Username string? null Authentication username
Password string? null Authentication password
UseTls bool false Enable TLS/SSL
CleanSession bool true Start with clean session
KeepAliveSeconds ushort 60 Keep alive interval
AutoReconnect bool true Auto reconnect on disconnect
ReconnectDelayMs int 5000 Reconnect delay in milliseconds

Broker Options

Option Type Default Description
BindAddress string 0.0.0.0 IP address to bind to
Port int 1883 Listen port
UseTls bool false Enable TLS
TlsPort int 8883 TLS port
MaxConnections int 10000 Max concurrent connections
AllowAnonymous bool true Allow anonymous connections
EnableRetainedMessages bool true Enable retained messages
EnablePersistentSessions bool true Enable persistent sessions

Authentication

// Simple username/password authentication
var authenticator = new SimpleAuthenticator()
    .AddUser("admin", "password")
    .AddUser("user1", "pass123");

broker.Authenticator = authenticator;

Custom Authentication

public class MyAuthenticator : IMqttAuthenticator
{
    public async Task<bool> AuthenticateAsync(
        string clientId,
        string? username,
        string? password,
        CancellationToken cancellationToken)
    {
        // Your authentication logic
        return await ValidateCredentialsAsync(username, password);
    }
}

TLS Configuration

Client

var options = new MqttClientOptions
{
    Host = "broker.example.com",
    Port = 8883,
    UseTls = true,
    ClientCertificate = new X509Certificate2("client.pfx", "password"),
    SkipCertificateValidation = false // Set true for self-signed certs
};

Broker

var options = new MqttBrokerOptions
{
    UseTls = true,
    TlsPort = 8883,
    ServerCertificate = new X509Certificate2("server.pfx", "password"),
    RequireClientCertificate = false
};

Quality of Service

Level Name Description
0 At most once Fire and forget, no acknowledgment
1 At least once Acknowledged delivery, may duplicate
2 Exactly once Assured delivery, no duplicates
// QoS 0 - Fire and forget
await client.PublishAsync("topic", "message", MqttQualityOfService.AtMostOnce);

// QoS 1 - At least once
await client.PublishAsync("topic", "message", MqttQualityOfService.AtLeastOnce);

// QoS 2 - Exactly once
await client.PublishAsync("topic", "message", MqttQualityOfService.ExactlyOnce);

Topic Wildcards

Wildcard Description Example
+ Single level sensors/+/temperature
# Multi level sensors/#
// Match: sensors/living-room/temperature, sensors/bedroom/temperature
await client.SubscribeAsync("sensors/+/temperature");

// Match: sensors/*, sensors/room1/*, sensors/room1/temp, etc.
await client.SubscribeAsync("sensors/#");

Will Message (LWT)

var options = new MqttClientOptions
{
    Host = "localhost",
    WillMessage = MqttApplicationMessage.Create(
        topic: "clients/my-client/status",
        payload: "offline",
        qos: MqttQualityOfService.AtLeastOnce,
        retain: true
    )
};

Building

# Build all projects
dotnet build MQTT.slnx

# Run broker sample
dotnet run --project samples/MqttBroker.Sample

# Run client sample (in another terminal)
dotnet run --project samples/MqttClient.Sample

Requirements

  • .NET 6.0, .NET 8.0, or .NET 10.0
  • Visual Studio 2022 17.10+ (for slnx support) or VS Code with C# Dev Kit

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MQTT-hnlyf:

Package Downloads
MQTT.Broker

A high-performance MQTT broker library for .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 187 1/12/2026