Chd.Library.MQTT
9.0.3
See the version list below for details.
dotnet add package Chd.Library.MQTT --version 9.0.3
NuGet\Install-Package Chd.Library.MQTT -Version 9.0.3
<PackageReference Include="Chd.Library.MQTT" Version="9.0.3" />
paket add Chd.Library.MQTT --version 9.0.3
#r "nuget: Chd.Library.MQTT, 9.0.3"
// Install Chd.Library.MQTT as a Cake Addin #addin nuget:?package=Chd.Library.MQTT&version=9.0.3 // Install Chd.Library.MQTT as a Cake Tool #tool nuget:?package=Chd.Library.MQTT&version=9.0.3
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
- Getting Started
- Usage
- Create MQTT Server and Client
- Create MQTTClientService which will inherit from MQTTClientServiceBase
- Injection MQTT Into The Project
- Apsettings Configurations
- Testing MQTT Server and Client Using MQTTBox
- Sending Messages to MQTT Server
- Authors
- Acknowledgments
🧐 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
- Mehmet Yoldaş - Linkedin
See also the list of contributors who participated in this project.
🎉 Acknowledgements
Thank you for using my library.
Product | Versions 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. |
-
net9.0
- Chd.Library.Logging (>= 8.0.9)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.CommandLine (>= 8.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 8.0.0)
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- MQTTnet (>= 4.3.3.952)
- MQTTnet.Extensions.ManagedClient (>= 4.3.3.952)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.