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
824 stars 153 forks source link

com.hivemq.client.mqtt.exceptions.MqttDecodeException: Exception while decoding CONNACK: remaining length too short #605

Closed Hussain-Badshah closed 7 months ago

Hussain-Badshah commented 7 months ago

🐛 Bug Report

I have an MQTT 3 broker server and a HiveMQ MQTT 5 client that tries to connect to the broker but fails with the following errors:

**com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5DisconnectException: Exception while decoding CONNACK: remaining length too short

Caused by: com.hivemq.client.mqtt.exceptions.MqttDecodeException: Exception while decoding CONNACK: remaining length too short**

Apparently this is a known issue according to: https://github.com/hivemq/hivemq-mqtt-client/issues/327 and HiveMQ team fixed the same in some 1.2 version, as mentioned in the same github comment thread. But the same error can be seen with HiveMQ MQTT 5 Client, version: 1.3.2

🔬 How To Reproduce

Steps to reproduce the behavior:

  1. Start the MQTT 3 server, I have docker image: hivemq/hivemq3:latest and hivemq/hivemq3:3.4.7
  2. Create an Async MQTT Java client and try to connect it to the broker. It fails while connecting.

Code sample

Code to create the Client: MqttClient.builder().useMqttVersion5().transportConfig() .mqttConnectTimeout(mqttClientConfig.getMqttClientConnectTimeoutInMillis(), TimeUnit.MILLISECONDS) .applyTransportConfig().addConnectedListener(listner -> { log.info("MQTT client: {} connected successfully.", mqttClientConfig.getClientId()); }) .addDisconnectedListener(listener -> { log.info("MQTT client: {} disconnected from the broker", mqttClientConfig.getClientId()); }).identifier(clientId) .serverHost(mqttBrokerUrl) .serverPort(mqttServerPort).buildAsync();

Code to Connect the client: var mqttClientConnectBuilder = mqtt5Client.connectWith().keepAlive(mqttClientConfig.getMqttClientKeepAliveInterval()); if (!mqttClientConfig.getCleanSession()) { mqttClientConnectBuilder = mqttClientConnectBuilder.cleanStart(mqttClientConfig.getCleanSession()) .sessionExpiryInterval(mqttClientConfig.getSessionExpiryInterval()); } mqttClientConnectBuilder.simpleAuth().username(mqttUsername) .password(mqttPassword.getBytes(StandardCharsets.UTF_8)) .applySimpleAuth().send().whenComplete((connAck, exception) -> { if (exception != null) { log.info("Failed connecting MQTT client: {} to broker. Exception is: {} Retrying connecting...", clientId, exception); } else { log.info( "MQTT client: {} connected successfully to the broker: {}, at port: {}, session present for this client: {}", clientId, mqttBrokerUrl, mqttServerPort, connAck.isSessionPresent()); } });

Environment

Where are you running/using this client? - On my local machine, I am starting a docker container of HiveMQ MQTT broker's image and then I have an MQTT application in Java in which I am creating a client and connecting it to the broker.

Hardware or Device? - NA

What version of this client are you using? - 1.3.2

JVM version? Answer - openjdk version "11.0.20.1" 2023-08-24 OpenJDK Runtime Environment (build 11.0.20.1+1-post-Ubuntu-0ubuntu120.04) OpenJDK 64-Bit Server VM (build 11.0.20.1+1-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Operating System?

Which MQTT protocol version is being used?

Which MQTT broker (name and version)?

HiveMQ Server logs

📈 Expected behavior

My MQTT 5 client should connect to the broker without fail.

📎 Additional context

A quick help to resolve this issue would be really appreciated. Thanks in advance.

sauroter commented 7 months ago

This is a crosspost from the HiveMQ Community forum thread for the same question. This is done to help other persons, that may not be in that community, but might have the same question.

Hi @Hussain-Badshah,

The HiveMQ Java clients supports MQTT 5 and MQTT 3.1.1 as stated in the README.md first sentence.

MQTT 5.0 and 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support.

HiveMQ (Broker) version 3 supports MQTT 3.1 and MQTT 3.1.1 and not MQTT 5.

HiveMQ (Broker) version 4 supports MQTT 3.1 and MQTT 3.1.1 and MQTT 5.

If you now like to connect with the HiveMQ Java client to a HiveMQ 3 broker that does not support MQTT 5 you will need to tell the client to create an MQTT 3 connection. (As the CONNECT packet is slightly different and the HiveMQ 3 Broker does not know how to handle an MQTT 5 style CONNECT packet.)

To do this you create the HiveMQ Java client in the MQTT 3 flavor as documented here:

Mqtt3Client client = Mqtt3Client.builder()
        .identifier(UUID.randomUUID().toString())
        .serverHost("<your broker address>")
        .build();

I hope this helps

All the best Georg