HiveMQ MQTT C# Client (BETA)
HiveMQ created the MQTT C# Client as part of our larger efforts to offer the MQTT community reliable, tested, performant, and maintained MQTT clients. No longer are you limited to choosing from anonymous third-party MQTT clients who aren’t maintained, updated, or supported. HiveMQ aims to provide the community with clients with a clear path for features, fixes, and support - critical requirements to assure your devices are always functioning at full potential and your business is running without issue.
Introduction to the HiveMQ MQTT C# Client
|HiveMQ MQTT C# Client
|C# for .NET 6.0 and greater
|Apache License 2.0
The new MQTT client for C# is in BETA, open source (available on Github), and licensed under the permissive Apache 2.0 License. We’re proud to open source this project for transparency and to welcome community input or contributions. The client currently features full MQTT 5.0 support and aims to be compatible with all major MQTT brokers - regardless of vendor. Notable features of the client are an extensive event system that allows you to hook into any client functionality, from message handling down to packet delivery. As a feature of MQTT 5.0, it also supports back pressure handling to manage the load on your devices by communicating throughput limits. TLS, basic authentication and all Quality of Service (QoS) levels are supported - and more is to come. As this client matures, this post will be updated to reflect the latest changes and additions.
Key features of the HiveMQ C# Client
How to use the HiveMQ MQTT C# Client
The HiveMQ MQTT C# Client is available as a Nuget package hosted on NuGet.org. To install:
Creating the Client
The HiveMQ MQTT C# Client can be instantiated with smart defaults as simply as:
Instantiating the client with default settings as in the previous example assumes an MQTT broker on localhost:1883. But if you need to override that address, there is the HiveMQClientOptions class:
Passing in the HiveMQClientOptions structure, you can customize client settings and behaviour. This class has many options available; see all the options available.
Connecting with Authentication
This example shows how to provide simple authentication credentials:
Connecting with an After Event
You can hook into the AfterConnect event to take any action as follows. Similar events exist for Before/AfterSubscribe, OnMessageReceived, OnDisconnectReceived and many more.
In order to build and execute a publish, you would use code similar to the below:
When creating a subscription, you provide an event callback handler which is used for each incoming message via the OnMessageReceived event:
See also: HiveMQClient events
To unsubscribe from a topic, simply call UnsubscribeAsync with the topic to unsubscribe from
If you want to view your existing subscriptions, they are always stored in client.Subscriptions.
Just call client.DisconnectAsync() on the client object created previously.
SSL / TLS
In order to use SSL / TLS for connecting to the broker, you simply specify its usage while building the client:
References & See Also
The HiveMQtt Client for C# on .NET
- The source code on Github: github.com/hivemq/hivemq-mqtt-client-dotnet
- Source Documentation: fuget.org/packages/HiveMQtt
- The HiveMQtt NuGet Package: nuget.org/packages/HiveMQtt
HiveMQ Support & Community
- HiveMQ Community Forum: community.hivemq.com
Other HiveMQ MQTT Clients
This is just the beginning of the story for HiveMQ and C#. We welcome you to try out the new client, browse the source code and contact us if you have any issues, comments or questions.