SRF.Network.Mqtt 0.9.3

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

DI wrapper for MQTTnet Client

Context

SRF.Network.Mqtt aims to provide simple use of MQTTnet with dependency injection.

It's minimalistic in flexibility and for trivial use cases only. It was developed as a service in a particular application before carving the code out as a dedicated library.

Present code allows only 1 broker to be connected to. In order to connect to multiple brokers, the library would need to be expanded e.g. relying on keyed singleton services and a logic to select the configuration section according the service registration key.

Package

dotnet add package SRF.Network.Mqtt

Usage

during app setup:

use SRF.Network.Mqtt.Hosting;

IHostBuilder hostBuilder = ...
hostBuilder.AddMqtt()

The configurable properties can be found in SRF.Network.Mqtt.MqttOptions. A minimalistic config section in appsettings.json or an other suitable place may look as follows:

    "Mqtt": {
        "Host": "my.broker.host.org",
        "UseTls": false,
        "User": "...",
        "Pass": "..."
    }

then inject IMqttBrokerConnection to your services.

To publish messages, use

public class MyPublishingLogic(IMqttBrokerConnection brokerConnection)
{
    readonly IMqttBrokerConnection brokerConn = brokerConnection;

    public void SendIt<TObject>(TObject message) where TObject : class
    {
        var topic = "my/topic/for/message";
        var publishingQueueItem = brokerConn.PublishJson(topic, message);
        publishingQueueItem.Published += MessagePublishedHandler; // can be skipped
    }

    private void MessagePublishedHandler(object sender, PublishEventArgs args)
    {
        // ...
    }
}

The Publish and PublishJson methods of the MqttBrokerConnection implementation of IMqttBrokerConnection are non-blocking. They queue the item for subsequent publishing by another Task / Thread.

Use an IPublisher implementation passed to the corresponding IMqttBrokerConnection.Publish method for fine-grained control of the publishing details. The following IPublisher implementations are available in the library (as of 0.9.0): PublisherString, PublisherJson

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.9.3 211 10/2/2025