HiveMQtt 0.5.0

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

// Install HiveMQtt as a Cake Tool
#tool nuget:?package=HiveMQtt&version=0.5.0                

alternate text is missing from this package README image alternate text is missing from this package README image

The Spectacular (BETA) C# MQTT Client for .NET

GitHub release (latest by date) GitHub Workflow Status Nuget GitHub

This .NET MQTT client was put together with love from the HiveMQ team but is still in DEVELOPMENT. As such some things may not work completely until it matures and although unlikely, APIs may change slightly before version 1.0.

We'd appreciate any feedback you have. Happy MQTT adventures!

  • Easy-to-Install: Available as a Nuget package.
  • Opensource: No blackbox code. Only trusted, tested and reviewed opensource code.
  • Easy to Use: Smart defaults, excellent interfaces and intelligent automation makes implementing a breeze.
  • MQTT v5.0 compatible: Backported versions 3.1.1 & 3.0 coming soon!
  • Extensive Event System: Hook into all parts of the client down to the packet level with built in events.
  • Globally Compatible: Built to be a fully compliant client compatible with all reputable MQTT brokers.
  • Actively Maintained: Built by the MQTT professionals that built HiveMQ (and do this for a living).
  • Extensively Documented: What good is it without excellent documentation?
  • Supported: Contact us anytime in this repository, in the community forum or through support.

Do you have a success story with this client? Let us know. We'd love to feature your story in a blog post or video and you'll get some sweet HiveMQ swag (and publicity) along the way.

What is this?

MQTT is an open standard protocol for publishing and consuming messages from IoT devices all the way up to mainframes. It's binary, massively performant and easy to use.

This client library is used to publish and consume messages over MQTT. So you can get a the temperature from a remote sensor, send a control message to a factory robot, tunnel WhatsApp messages to a Twitter account or anything else you can imagine.

This is the client library that speaks with an MQTT broker that delivers messages to their final destination.

Need a broker? Sign up for a free broker at HiveMQ Cloud and be up and running in a couple minutes. Connect up to 100 devices - no credit card required.

MQTT Resources

Need an MQTT Broker?

This client communicates with an MQTT broker to publish and consume messages. It's built to be compatible with all major MQTT brokers but if you need a broker now run the HiveMQ Community Edition:

docker run --name hivemq-ce -d -p 1883:1883 hivemq/hivemq-ce

This will run the HiveMQ Community Edition broker on localhost port 1883.

If you need advanced features, checkout our premium editions or alternatively HiveMQ Cloud which is free to connect up to 100 devices (no credit card required).

Install

This package is available on NuGet.org and can be installed with:

dotnet add package HiveMQtt

See the HiveMQtt NuGet page for more installation options.

Quickstart

Simple Connect

using HiveMQtt.Client;

// Connect
var client = new HiveMQClient();
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
With Options
var options = new HiveMQClientOptions();
options.Host = 'candy.x39.eu.hivemq.cloud';
options.Port = 8883;

var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);

Basic Subscribe & Publish

using HiveMQtt.Client;

// Connect
var client = new HiveMQClient();
var connectResult = await client.ConnectAsync().ConfigureAwait(false);

// Message Handler
client.OnMessageReceived += (sender, args) =>
{
    Console.WriteLine("Message Received: {}", args.PublishMessage.PayloadAsString)
};

// Subscribe
await client.SubscribeAsync("instrument/x9284/boston").ConfigureAwait(false);

await client.PublishAsync(
                "core/dynamic_graph/entity/227489", // Topic to publish to
                "{'2023': '👍'}"                    // Message to publish
                ).ConfigureAwait(false);

Last Will and Testament

The Last Will and Testament support of MQTT can be used to notify subscribers that your client is offline.

For a more in-depth explanation, see What is MQTT Last Will and Testament (LWT)? – MQTT Essentials: Part 9.

// Specify the Last Will and Testament specifics in HiveMQClientOptions
var options = new HiveMQClientOptions
{
    LastWillAndTestament = new LastWillAndTestament("last/will", QualityOfService.AtLeastOnceDelivery, "last will message"),
};

// Optionally set extended properties on the Last Will and Testament message
options.LastWillAndTestament.WillDelayInterval = 1;
options.LastWillAndTestament.PayloadFormatIndicator = 1;
options.LastWillAndTestament.MessageExpiryInterval = 100;
options.LastWillAndTestament.ContentType = "application/text";
options.LastWillAndTestament.ResponseTopic = "response/topic";
options.LastWillAndTestament.CorrelationData = new byte[] { 1, 2, 3, 4, 5 };
options.LastWillAndTestament.UserProperties.Add("userPropertyKey", "userPropertyValue");

// ConnectAsync will transmit the Last Will and Testament configuration.
var client = new HiveMQClient(options);
connectResult = await client.ConnectAsync().ConfigureAwait(false);

// The Last Will and Testament message will be sent to the "last/will" topic if your clients get
// unexpectedly disconnected or alternatively, if your client disconnects with `DisconnectWithWillMessage`
var disconnectOptions = new DisconnectOptions { ReasonCode = DisconnectReasonCode.DisconnectWithWillMessage };
var disconnectResult = await client.DisconnectAsync(disconnectOptions).ConfigureAwait(false);

Because the client above disconnected with DisconnectReasonCode.DisconnectWithWillMessage, subscribers to the last/will topic will receive the Last Will and Testament message as specified above.

More

For more examples that you can easily copy/paste, see our Examples.

There is even an https://github.com/hivemq/hivemq-mqtt-client-dotnet/tree/main/Examples/HiveMQtt-CLI to demonstrate usage of the package.

Configuration

Logging

The HiveMQtt package uses NLog and can be configured with a configuration file (NLog.config). Having this file in the same directory of your executable will configure the HiveMQtt logger to output as configured:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets>
    <target name="logfile" xsi:type="File" fileName="HiveMQtt.log" />
    <target name="logconsole" xsi:type="Console" />
  </targets>

  <rules>
     
    <logger name="HiveMQtt.*" minlevel="Error" writeTo="logconsole" />
  </rules>
</nlog>

Setting minlevel to Trace will output all activity in the HiveMQtt package down to packet and event handling. Using this level will produce a lot of output such as the following:

2023-10-04 16:56:54.9373|TRACE|HiveMQtt.Client.HiveMQClient|BeforeConnectEventLauncher
2023-10-04 16:56:55.0081|TRACE|HiveMQtt.Client.HiveMQClient|7: TrafficInflowProcessor Starting...Connecting
2023-10-04 16:56:55.0081|TRACE|HiveMQtt.Client.HiveMQClient|9: TrafficOutflowProcessor Starting...Connecting
2023-10-04 16:56:55.0081|TRACE|HiveMQtt.Client.HiveMQClient|--> ConnectPacket
2023-10-04 16:56:55.0128|TRACE|HiveMQtt.Client.HiveMQClient|OnConnectSentEventLauncher
2023-10-04 16:56:55.0374|TRACE|HiveMQtt.Client.HiveMQClient|<-- ConnAck
2023-10-04 16:56:55.0374|TRACE|HiveMQtt.Client.HiveMQClient|OnConnAckReceivedEventLauncher
2023-10-04 16:56:55.0379|TRACE|HiveMQtt.Client.HiveMQClient|AfterConnectEventLauncher

Other MQTT Clients

🛡 License

License

This project is licensed under the terms of the Apache Software License 2.0 license. See LICENSE for more details.

📃 Citation

@misc{hivemq-mqtt-client-dotnet,
  author = {HiveMQ GmbH},
  title = {The HiveMQ C# MQTT client for .NET},
  year = {2023},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/hivemq/hivemq-mqtt-client-dotnet}}
}
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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on HiveMQtt:

Package Downloads
MQContract.HiveMQ

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.24.0 174 11/18/2024
0.23.0 5,829 9/6/2024
0.22.1 1,292 7/22/2024
0.22.0 510 7/5/2024
0.21.0 101 7/2/2024
0.20.0 93 6/27/2024
0.19.1 104 6/24/2024
0.19.0 339 6/17/2024
0.18.1 389 6/10/2024
0.18.0 55 6/10/2024
0.17.0 461 6/4/2024
0.16.0 103 5/31/2024
0.15.1 113 5/29/2024
0.15.0 82 5/28/2024
0.14.0 219 5/21/2024
0.13.0 180 5/17/2024
0.12.0 256 5/7/2024
0.11.5 72 5/6/2024
0.11.4 1,869 4/16/2024
0.11.3 323 4/5/2024
0.11.2 146 4/3/2024
0.11.1 2,477 3/26/2024
0.11.0 544 3/21/2024
0.11.0-rc2 77 3/19/2024
0.10.3 1,905 2/23/2024
0.10.2 972 2/2/2024
0.10.1 257 1/23/2024
0.10.0 77 1/23/2024
0.9.0 380 1/15/2024
0.8.0 217 1/9/2024
0.7.0 263 12/28/2023
0.6.0 4,618 11/10/2023
0.5.0 741 10/30/2023
0.4.3 323 10/9/2023
0.4.1 196 9/28/2023
0.4.0 105 9/26/2023
0.3.0 4,404 9/7/2023
0.2.1 1,998 8/21/2023
0.2.0 1,677 6/13/2023
0.1.10 560 4/18/2023
0.1.9 163 4/12/2023
0.1.9-preview.0.2 98 4/11/2023
0.1.6 325 3/24/2023
0.1.5 211 3/24/2023
0.1.4 208 3/24/2023
0.1.3 266 2/22/2023
0.1.2 240 2/21/2023
0.1.1 296 2/21/2023