Publish WithExactlyOnceQoS with 2 Clients and 1 Broker in the same application results in disconnects
See original GitHub issueDescribe the bug
When publishing a message with QoS “WithExactlyOnceQoS” the Client gets disconnected immediately after the receipt of the message. QoS “WithAtMostOnceQoS” works as expected.
Which project is your bug related to?
- ManagedClient
- Server
To Reproduce
run program below, change QoS in Line 87
Expected behavior
Client should not disconnect after message receipt.
Screenshots
Expected

Current

using MQTTnet;
using MQTTnet.Client.Options;
using MQTTnet.Extensions.ManagedClient;
using MQTTnet.Server;
using System;
using System.Text;
using System.Threading.Tasks;
namespace MQTTnet_Test1
{
class Program
{
private IMqttServer LocalServer;
static void Main(string[] args)
{
var p = new Program();
p.StartServer();
var client1 = p.StartClient("client1");
var client2 = p.StartClient("client2");
Task.Delay(1000);
var message = new MqttApplicationMessageBuilder().WithTopic("topic1").WithPayload("Hello World").WithExactlyOnceQoS().WithRetainFlag().Build();
client1.PublishAsync(message);
Console.ReadLine();
}
private void StartServer()
{
var optionsBuilder = new MqttServerOptionsBuilder().WithConnectionBacklog(100).WithDefaultEndpointPort(1883);
LocalServer = new MqttFactory().CreateMqttServer();
LocalServer.StartAsync(optionsBuilder.Build());
LocalServer.UseClientConnectedHandler(evt =>
{
Console.WriteLine(string.Format("SERVER - Client connected {0}", evt.ClientId));
});
LocalServer.UseClientDisconnectedHandler(evt =>
{
Console.WriteLine(string.Format("SERVER - Client disconnected {0}", evt.ClientId));
});
LocalServer.UseApplicationMessageReceivedHandler(msgEvt =>
{
Console.WriteLine(string.Format("SERVER - Message received from: {0}, topic: {1}, content: {2}", msgEvt.ClientId, msgEvt.ApplicationMessage.Topic, msgEvt.ApplicationMessage.ConvertPayloadToString()));
});
}
private IManagedMqttClient StartClient(string clientid)
{
var options = new ManagedMqttClientOptionsBuilder()
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithClientOptions(new MqttClientOptionsBuilder().WithClientId(clientid).WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500).WithTcpServer("localhost").Build())
.Build();
var client = new MqttFactory().CreateManagedMqttClient();
client.UseApplicationMessageReceivedHandler(e =>
{
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
Console.WriteLine($"+ Client = {clientid}");
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
Console.WriteLine();
});
client.UseConnectedHandler(e =>
{
Console.WriteLine("Client connected successfully {0}", e);
});
client.UseDisconnectedHandler(e =>
{
Console.WriteLine("Client disconnected {0}", e);
});
client.StartAsync(options);
var topic = "topic1";
// -------------------- PROBLEM ------------------------------------------
// works
//client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(topic).WithAtMostOnceQoS().Build());
// doesn't work
client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(topic).WithExactlyOnceQoS().Build());
return client;
}
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
Publish WithExactlyOnceQoS with 2 Clients and 1 Broker ...
When publishing a message with QoS "WithExactlyOnceQoS" the Client gets disconnected immediately after the receipt of the message.
Read more >What is MQTT Quality of Service (QoS) 0,1, & 2?
If the subscribing client defines a lower QoS level than the publishing client, the broker will transmit the message with the lower QoS...
Read more >c# - MQTTnet PublishAsync Exception
I'm trying to use "MQTTnet" in a Xamarin application. It connects fine but when I try to publish anything, well, it publishs but...
Read more >Confusion about client-server connection establishment in ...
I come to this conclusion because if the client disconnects after subscription, then a server cannot forward messages to it because it is...
Read more >Using MQTT Over WebSockets with Mosquitto
MQTT over websockets allows you to send data to an MQTT broker from a web ... I tried adding next two code lines,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The issue is located in the server implementation of MQTTv5.
Hi, thanks for reporting this issue. It is a problem with MQTT v5 and will be fixed with the next version. I was able to reproduce it in a new Unit Test.