ConnectMQ.Mqtt
1.0.0
dotnet add package ConnectMQ.Mqtt --version 1.0.0
NuGet\Install-Package ConnectMQ.Mqtt -Version 1.0.0
<PackageReference Include="ConnectMQ.Mqtt" Version="1.0.0" />
<PackageVersion Include="ConnectMQ.Mqtt" Version="1.0.0" />
<PackageReference Include="ConnectMQ.Mqtt" />
paket add ConnectMQ.Mqtt --version 1.0.0
#r "nuget: ConnectMQ.Mqtt, 1.0.0"
#:package ConnectMQ.Mqtt@1.0.0
#addin nuget:?package=ConnectMQ.Mqtt&version=1.0.0
#tool nuget:?package=ConnectMQ.Mqtt&version=1.0.0
ConnectMQ.Mqtt
Publish and subscribe on the free public MQTT broker broker.connectmq.com in a few lines of C#, with no account needed. The package is a preconfigured MQTTnet client for TCP, TLS, WebSocket and secure WebSocket. It also works with private brokers created on connectmq.com.
dotnet add package ConnectMQ.Mqtt
Requires .NET 8 or later.
Quick start
using ConnectMQ.Mqtt;
await using var client = await ConnectMQClient.ConnectAsync(); // mqtts://broker.connectmq.com:8883
var prefix = ConnectMQBroker.NewTopicPrefix(); // e.g. "connectmq/demo/7f3a91"
await using var sub = await client.SubscribeAsync($"{prefix}/#", msg =>
Console.WriteLine($"{msg.Topic}: {msg.Payload}"));
await client.PublishAsync($"{prefix}/temperature", """{"value":21.5}""");
The public area is shared, so use a random prefix to keep your topics apart from other testers.
Check connectivity
var result = await ConnectMQBroker.TestAsync(MqttTransport.Tls);
Console.WriteLine(result.Ok
? $"OK, connect {result.ConnectTime?.TotalMilliseconds:0} ms, round-trip {result.RoundTripTime?.TotalMilliseconds:0} ms"
: $"Failed: {result.Error}");
API
ConnectMQClient
| Member | Description |
|---|---|
ConnectAsync(options?, ct) |
Connects and returns a client. Defaults: public broker, TLS, MQTT 5, random client ID |
PublishAsync(topic, string or byte[] payload, qos, retain, ct) |
Publishes a message. Publish an empty payload with retain: true to clear a retained message |
SubscribeAsync(filter or filters, handler, qos, ct) |
Calls handler for each matching message; dispose the result to unsubscribe |
WaitForMessageAsync(filter, timeout?, predicate?, ct) |
Waits for the next (matching) message; throws TimeoutException |
Mqtt |
The underlying MQTTnet IMqttClient |
Url, ClientId, IsConnected |
Connection details |
DisposeAsync() |
Sends DISCONNECT and closes |
Handlers are attached before the broker acknowledges the subscription, so retained messages are never missed.
ConnectMQMessage has Topic, Payload (UTF-8 string), RawPayload, Qos, Retain and Received.
ConnectMQOptions
| Property | Default |
|---|---|
Transport |
MqttTransport.Tls (Tcp 1883, Tls 8883, WebSocket 8083, SecureWebSocket 8084) |
Host |
broker.connectmq.com |
Port |
Standard port of the transport |
ClientId |
net- plus a random suffix |
Username, Password |
None (not needed on the public broker) |
ProtocolVersion |
MqttProtocolVersion.V500 |
KeepAlive, Timeout |
60 s, 10 s |
CleanSession |
true |
Configure |
Action<MqttClientOptionsBuilder> for last-will, certificates and other MQTTnet options |
ConnectMQBroker
| Member | Description |
|---|---|
Host |
"broker.connectmq.com" |
GetUrl(transport), GetPort(transport) |
Endpoint details |
NewTopicPrefix(root?) |
Random prefix such as connectmq/demo/7f3a91 |
TopicMatches(filter, topic) |
Wildcard matching for +, # and $share/... |
TestAsync(transport?, host?, ct) |
Connects, publishes, receives; returns BrokerTestResult |
Examples
QoS and retained messages
using MQTTnet.Protocol;
await client.PublishAsync("connectmq/demo/7f3a91/status", "online",
MqttQualityOfServiceLevel.AtLeastOnce, retain: true);
Request and reply
var reply = client.WaitForMessageAsync("connectmq/demo/7f3a91/reply", TimeSpan.FromSeconds(10));
await client.PublishAsync("connectmq/demo/7f3a91/request", "ping");
Console.WriteLine((await reply).Payload);
Background service
public sealed class SensorWorker : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await using var client = await ConnectMQClient.ConnectAsync(cancellationToken: stoppingToken);
while (!stoppingToken.IsCancellationRequested)
{
await client.PublishAsync("connectmq/demo/7f3a91/cpu", Random.Shared.Next(100).ToString(), cancellationToken: stoppingToken);
await Task.Delay(5000, stoppingToken);
}
}
}
Private ConnectMQ broker
await using var client = await ConnectMQClient.ConnectAsync(new ConnectMQOptions
{
Host = "YOUR_BROKER_HOST",
Transport = MqttTransport.Tls,
Username = Environment.GetEnvironmentVariable("MQTT_USERNAME"),
Password = Environment.GetEnvironmentVariable("MQTT_PASSWORD"),
});
Public broker
| Protocol | URL |
|---|---|
| MQTT (TCP) | mqtt://broker.connectmq.com:1883 |
| MQTT over TLS | mqtts://broker.connectmq.com:8883 |
| WebSocket | ws://broker.connectmq.com:8083/mqtt |
| Secure WebSocket | wss://broker.connectmq.com:8084/mqtt |
Access is anonymous, but the client ID must not be empty. Limits: 64 KB per message, 10 messages per second and 10 subscriptions per client, topics up to 8 levels deep, and sessions expire after at most 30 minutes.
Everything on the public broker is public. Any client can subscribe to
#. Use the broker only for testing, and always under a random topic prefix. For production, create a private broker with its own credentials at connectmq.com.
More: Free Public MQTT Broker for Testing. Also available for Node.js as @connectmq/mqtt and for PowerShell as MQTTBroker.
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- MQTTnet (>= 5.2.0.1603)
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 |
|---|---|---|
| 1.0.0 | 102 | 11/5/2026 |
Initial release: ConnectMQClient (connect, publish, subscribe, wait for message), ConnectMQBroker (endpoints, topic prefix, wildcard matching, connectivity test).