hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
854 stars 158 forks source link

Mqtt5DisconnectException: Exception while decoding PUBLISH: topic alias must not exceed topic alias maximum #631

Closed nishankhadka09 closed 3 months ago

nishankhadka09 commented 3 months ago

**

Broker: MQTTNet

**

**

How:

**

1-> Connect to broker from android device A. Success 2 -> Connect to broker from android device B. Success. 3 -> Publish from both device, one of the device disconnects

**

ConnAck Response:

Connected successfully MqttConnAck{reasonCode=SUCCESS, sessionPresent=false, restrictions=MqttConnAckRestrictions{receiveMaximum=65535, maximumPacketSize=268435460, topicAliasMaximum=65535, maximumQos=EXACTLY_ONCE, retainAvailable=true, wildcardSubscriptionAvailable=true, sharedSubscriptionAvailable=false, subscriptionIdentifiersAvailable=true}}.

Disconnection Reason for Device A:

**

com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5DisconnectException:Exception while decoding PUBLISH: topic alias must not exceed topic alias maximum Caused by: com.hivemq.client.mqtt.exceptions.MqttDecodeException: Exception while decoding PUBLISH: topic alias must not exceed topic alias maximum

**

nishankhadka09 commented 3 months ago

Solved it by setting `topicAliasMaximum() restricition

(mqttClient as Mqtt5Client?)?.toAsync()?.connectWith()?.simpleAuth()
                ?.username(mqttConfigFlow.value.userName)
                ?.password(UTF_8.encode(mqttConfigFlow.value.password))?.applySimpleAuth()
                ?.restrictions()?.topicAliasMaximum(65535)
                ?.applyRestrictions()?.send()

by default Topic Aliases are enabled in Hive client - and number of max topicAliases allowed is 1, when broker sent a message on another topic with topicAlias value of 2 , the hive client would disconnect saying

"Exception while decoding PUBLISH: topic alias must not exceed topic alias maximum".

setting only topicAliasMaximum to 0 - gave me the same exception, setting sendTopicAliasMaximumto 0 as well fixed the disconnection issue, but again, the callback for subscribed topic would be triggered twice. Would love to get an explanation for this behaviour.

FInally set it to 65535, and the issue got fixed.

bddckr commented 3 months ago

I believe everything you've reported here points to the broker not being MQTT v5 spec-compliant. Quoting the relevant portions:

This value indicates the highest value that the Client will accept as a Topic Alias sent by the Server. The Client uses this value to limit the number of Topic Aliases that it is willing to hold on this Connection. The Server MUST NOT send a Topic Alias in a PUBLISH packet to the Client greater than Topic Alias Maximum [MQTT-3.1.2-26]. A value of 0 indicates that the Client does not accept any Topic Aliases on this connection. If Topic Alias Maximum is absent or zero, the Server MUST NOT send any Topic Aliases to the Client [MQTT-3.1.2-27]. https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901051

So tl;dr: This MQTT client is behaving as it should. MQTTNet doesn't seem to be adhering to the spec.