Closed andig closed 10 months ago
I would expect that- when Subscribe() token handling completes- the callback will have been invoked if a retained message exists and no error occured. That seems not always to be the case.
Your expectation is incorrect; the Subscribe
token completes when a SUBACK
packet is received (indicating that the server has processed the subscription request). THE MQTT spec states:
The Server is permitted to start sending PUBLISH packets matching the Subscription before the Server sends the SUBACK Packet.
and
Any existing retained messages matching the Topic Filter MUST be re-sent, but the flow of publications MUST NOT be interrupted [MQTT-3.8.4-3].
It does not state whether retained messages are sent before or after the SUBACK
. In your example the messages are being received after the SUBACK
.
There used to be a similar issue in the java version: https://github.com/eclipse/paho.mqtt.java/issues/432.
That was quite a different issue (the Java client was not passing messages received before the SUBACK
was received onto the appropriate handler; this client registers the handler before sending the SUBSCRIBE
request).
Subscribe() has no way to specify retain- should this matter?
This is just a generic record of the packet fixed header (the flag only has meaning for PUBLISH packets).
Note: As you are subscribing with QOS1 be mindful of the potential issue mentioned in the readme:
When QOS1+ subscriptions have been created previously and you connect with CleanSession set to false it is possible that the broker will deliver retained messages before Subscribe can be called. To process these messages either configure a handler with AddRoute or set a DefaultPublishHandler. If there is no handler (or DefaultPublishHandler) then inbound messages will not be acknowledged. Adding a handler (even if it's opts.SetDefaultPublishHandler(func(mqtt.Client, mqtt.Message) {})) is highly recommended to avoid inadvertently hitting inflight message limits.
Thank youl @MattBrittan for the detailed explanation. I admit not being accustomed with the spec. The specified behavior seems unfortunate though. As far as I understand, there is no way for a client to identify if a server has a retained value waiting for him other that wait- how long would depend on server load, network, etc.
Much appreciated, and Merry Christmas š š».
As far as I understand, there is no way for a client to identify if a server has a retained value waiting for him other that wait- how long would depend on server load, network, etc.
Correct; retained messages provide initial values when a connection is established; there is no real provision for systems to connect, get retained messages and disconnect (i.e. the MQTT protocol provides no simple mechanism for this).
I'll close this off as I believe your question has been answered; have a great Christmas!
I'm listening to topics with retained messages:
I would expect that- when
Subscribe()
token handling completes- the callback will have been invoked if a retained message exists and no error occured. That seems not always to be the case.There used to be a similar issue in the java version: https://github.com/eclipse/paho.mqtt.java/issues/432.
Here is a related log:
I'm wondering what this means:
Subscribe()
has no way to specifyretain
- should this matter?