Chd.Library.MQTT 9.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chd.Library.MQTT --version 9.0.5                
NuGet\Install-Package Chd.Library.MQTT -Version 9.0.5                
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="Chd.Library.MQTT" Version="9.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chd.Library.MQTT --version 9.0.5                
#r "nuget: Chd.Library.MQTT, 9.0.5"                
#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.
// Install Chd.Library.MQTT as a Cake Addin
#addin nuget:?package=Chd.Library.MQTT&version=9.0.5

// Install Chd.Library.MQTT as a Cake Tool
#tool nuget:?package=Chd.Library.MQTT&version=9.0.5                

MQTT library for .Net Core

Chd (cleverly handle difficulty) library helps you cleverly handle difficulty, writing code fastly and do your application stable.

📝 Table of Contents

🧐 About

MQTT is a lightweight publish/subscribe messaging protocol designed for M2M (machine to machine) communication. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios. It is also ideal for mobile applications because of its small size, low power usage, minimum bandwidth, and low data packets.

🏁 Getting Started

We will create a new ASP.NET Core Web API project.

Prerequisites

You must use .net core 9.0 or higher

🎈 Usage

Create MQTT Server and Client

We will create a new MQTT Server and Client. We will use the MQTT Server to send messages to all clients. We will use the MQTT Client to receive messages from the server. We will create a class that will inherit from MQTTClientServiceBase and override the OnMessageReceived method.

Create MQTTClientService which will inherit from MQTTClientServiceBase

We will create a new class that will inherit from MQTTClientServiceBase. We will override the OnMessageReceived method.

public class MQTTClientService : MQTTClientServiceBase
{
    public override void OnMessageReceived(string message)
    {
        Console.WriteLine(message);
    }
}

💉 Injection MQTT Into The Project

We will inject our MQTT Server and Client into the project. We will use the MQTT Server to send messages to all clients. We will use the MQTT Client to receive messages from the server.

   var builder = WebApplication.CreateBuilder(args);
   ....
   ....
   builder.Services.AddMQTTServer<MQTTClientService, MQTTServerServiceBase>("CHD");// CHD is the topic name. You may change it.

⚙️ Apsettings Configurations

You must add code below in appsettings.json

  "MQTT": {
    "UserName": "CHD",
    "Password": "library",
    "Host": "localhost",
    "Port": "6506"
  }

🔧 Testing MQTT Server and Client Using MQTTBox

We will use MQTTBox to test our MQTT Server and Client. Download and install MQTTBox. Enter the MQTT Server URL and port number. We will use the MQTT Server URL and port number to connect to the MQTT Server. We will use the topic name to subscribe to the topic. We will use the topic name to publish messages to the topic. Add breakpoints to the OnMessageReceived method in the MQTTClientService class. We will publish messages to the topic using MQTTBox. We will receive messages in the OnMessageReceived method in the MQTTClientService class.

🏹 Sending Messages to MQTT Server

We will send messages to all clients using the SendMessage method in controller.

    public class MQTTTestController : ControllerBase
    {
        public MqttClientOptions _mqttClientOptions { get; }
        public MQTTConfig MQTTConfig { get; }

        public MQTTTestController(MqttClientOptions mqttClientOptions)
        {
            _mqttClientOptions = mqttClientOptions;
        }
        [Route("SendMessage")]
        [HttpGet]
        public string? SendMessage()//For testing on swagger or etc..
        {
            MQTTUtils.SendMessage(_mqttClientOptions, "CHD", "Hello World");
            return "Ok";
        }
    }

✍️ Authors

See also the list of contributors who participated in this project.

🎉 Acknowledgements

Thank you for using my library.

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. 
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
9.0.6 67 1/30/2025
9.0.5 64 1/30/2025
9.0.4 71 1/17/2025
9.0.3 72 1/17/2025
9.0.2 73 1/16/2025
9.0.1 115 1/4/2025
9.0.0 115 1/4/2025