question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Publish WithExactlyOnceQoS with 2 Clients and 1 Broker in the same application results in disconnects

See original GitHub issue

Describe 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

2020-11-04_09h25_30

Current

2020-11-04_09h26_38

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:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
chkr1011commented, Nov 5, 2020

The issue is located in the server implementation of MQTTv5.

1reaction
chkr1011commented, Nov 4, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found