shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

How to recall a time-out message? #89

Closed yancaidev closed 5 months ago

yancaidev commented 5 months ago

I am using the WebSocket Mqtt5Client to connect to an AWS IoT server. I want to achieve the following functionality: After publishing a QoS1 message that exceeds 3 seconds, it should no longer be sent to the broker. How can I implement this?

shamblett commented 5 months ago

What do you mran by exceeds 3 seconds? Do you meam you dont get a pub ack in 3 seconds?

If so there is mothing you can do at the client end, as far as the client is concerned it has been sent but not acked.

Where has this requirement come from? Doesnt sounf like an MQTT requirement.

yancaidev commented 5 months ago

Yes, my intention is that if a pub ack is not received after 3 seconds, it should be considered a timeout. Why do we need this feature? Consider the following scenario: When a user wants to remotely turn on the lights at home temporarily, but their network is poor or even unavailable, they have to wait a long time to receive a response from the broker. If the user doesn’t want to wait and exits the page, they might think the lights didn’t turn on. However, the message to turn on the lights will still be sent to the broker.

So, I think that if a message times out and is not sent to the broker, allowing the user self to resend it would be more user-friendly.

May this deviate from the design of MQTT?

Disconnecting the connection and reconnect might be feasible, but this approach seems rather clumsy.

Can you give me some advice? Thank you.

shamblett commented 5 months ago

How can it time out if its not sent to the broker? If you don't send it you will never know if you are going to get a pub ack in 3 seconds or not as the broker will not receive the message, so will send nothing. You have to send it to see if/when you get an ack at all.

You could use the published stream(see the API), when a message that needs a handshake(QoS1/2) is sent to the broker the message is placed on the published stream when the handshake is complete. In your case publish your message then listen on the stream, if your message does not appear within 3 seconds then you can do whatever processing is needed.

yancaidev commented 5 months ago

Thank you for pointing " there is mothing you can do at the client end, as far as the client is concerned it has been sent but not acked." out.

After careful consideration, setting a short keep-alive time on the client, such as 3 seconds, to inform users of poor network conditions and encourage them to improve their network environment, may be a better approach.