Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MqttClient Publish a message when you are inside the InterceptingPublishAsync result to an Error when the Client Publish a QoS 1 #1969

Open
miaooss opened this issue Apr 16, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@miaooss
Copy link

miaooss commented Apr 16, 2024

Note: I'm migrating from version 3.1.2 to 4.3.3.952

Describe the bug

I have a device that is connecting to Mqtt,
The device is subscribing and publishing a message correctly.

But publishing a message from the server will be ignored if the PubAck is not sent.

Here is my setup,
I'm using the InterceptingPublishAsync event to intercept messages from the client.
In one of the messages I received, I would like to send another publish on a specific topic.
Unfortunately, the pubAck will be sent after my event inside the InterceptingPublishAsync
The Client will ignore the Message (The Device/Client is not using MQTTNET, but using a low-level library)

We can find the issue here:

async Task HandleIncomingPublishPacket(MqttPublishPacket publishPacket, CancellationToken cancellationToken)

Having another event after the end of a pubAck, will help handling the message. OnCompletedPublishAsync

Description of the QoS 1 delivery protocol

In the QoS 1 delivery protocol, the Sender

· MUST assign an unused Packet Identifier each time it has a new Application Message to publish.

· MUST send a PUBLISH Packet containing this Packet Identifier with QoS=1, DUP=0.

· MUST treat the PUBLISH Packet as “unacknowledged” until it has received the corresponding PUBACK packet from the receiver. See Section 4.4 for a discussion of unacknowledged messages.

image

1 The receiver is not required to complete delivery of the Application Message before sending the PUBACK. When its original sender receives the PUBACK packet, ownership of the Application Message is transferred to the receiver

Which component is your bug related to?

  • QoS 1 delivery protocol
  • MqttClient

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of MQTTnet '4.3.3.952'.
  2. Subscribe to the server with InterceptingPublishAsync
  3. Publish a message inside the event method
  4. The Error can be seen with WireShark

Expected behavior

The pubAck should be sent before I even handle the business logic of the event itself

Screenshots

MqttNet 3
image

MqttNet 4
image

Code example

using MQTTnet;
using MQTTnet.Server;

var mqttFactory = new MqttFactory();

var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();

using (var mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions))
{
    await mqttServer.StartAsync();

    mqttServer.InterceptingPublishAsync += async (InterceptingPublishEventArgs arg) =>
    {
        // Create a new message using the builder as usual.
        var message = new MqttApplicationMessageBuilder().WithTopic("HelloWorld").WithPayload("Test").Build();

        // Now inject the new message at the broker.
        await mqttServer.InjectApplicationMessage(
            new InjectedMqttApplicationMessage(message)
            {
                SenderClientId = "SenderClientId"
            });
    };

    Console.ReadLine();

    // Stop and dispose the MQTT server if it is no longer needed!
    await mqttServer.StopAsync();
}
@miaooss miaooss added the bug Something isn't working label Apr 16, 2024
@miaooss miaooss changed the title QoS 1 MqttClient Publish a message when you are inside the InterceptingPublishAsync result to an Error when the Client Publish a QoS 1 Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant