Home

Awesome

The Spectacular C# MQTT Client for .NET

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

Static Badge Static Badge Static Badge

💽 Installation & Compatibility

🚀 Features

🏝️ Ease of Use

🛟 Maintenance and Support

🐧 Opensource

📍 Get Started Today

Download the HiveMQ C# MQTT Client for .NET and start building your next-generation IoT, industrial automation, or real-time data streaming application with HiveMQ on your side.

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.

Example

The following illustrates the client pattern to connect, subscribe and publish messages.

using HiveMQtt.Client;
using HiveMQtt.MQTT5.Types;

// Setup Client options and instantiate
var options = new HiveMQClientOptionsBuilder().
                    WithBroker("candy.x39.eu.hivemq.cloud").
                    WithPort(8883).
                    WithUseTls(true).
                    Build();
var client = new HiveMQClient(options);

// Setup an application message handlers BEFORE subscribing to a topic
client.OnMessageReceived += (sender, args) =>
{
    Console.WriteLine("Message Received: {}", args.PublishMessage.PayloadAsString);
};

// Connect to the MQTT broker
var connectResult = await client.ConnectAsync().ConfigureAwait(false);

// Configure the subscriptions we want and subscribe
var builder = new SubscribeOptionsBuilder();
builder.WithSubscription("topic1", QualityOfService.AtLeastOnceDelivery)
       .WithSubscription("topic2", QualityOfService.ExactlyOnceDelivery);
var subscribeOptions = builder.Build();
var subscribeResult = await client.SubscribeAsync(subscribeOptions);

// Publish a message
var publishResult = await client.PublishAsync("topic1/example", "Hello Payload");

For a Quickstart, more examples and walkthroughs, see the documentation.

Other MQTT Clients

For a list of all known MQTT clients, see MQTT.org.

🛡 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 = {2024},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/hivemq/hivemq-mqtt-client-dotnet}}
}