0

In a Java server application, we want to use the correlationData, which is part of MQTT5, so we can use this in the reply message to link and validate it when the reply is received.

I'm using the hivemq-mqtt-client library 1.2.2 and connecting to HiveMQ Cloud.

The connection is made like this:

private Mqtt5AsyncClient client;

public MqttConfig(Environment environment) {
    client = MqttClient.builder()
            .useMqttVersion5()
            .identifier("TestServer")
            .serverHost(MQTT_SERVER)
            .serverPort(MQTT_PORT)
            .sslWithDefaultConfig()
            .automaticReconnectWithDefaultConfig()
            .buildAsync();

    client.connectWith()
            .simpleAuth()
            .username(MQTT_USER)
            .password(MQTT_PASSWORD.getBytes())
            .applySimpleAuth()
            .send()
            .whenComplete((connAck, throwable) -> {
                if (throwable != null) {
                    logger.error("Could not connect to MQTT: {}", throwable.getMessage());
                } else {
                    logger.info("Connected to MQTT: {}", connAck.getReasonCode());
                }
            });
}

public Mqtt5AsyncClient getClient() {
    return client;
}

Sending a message is done with this method:

mqttConfig.getClient()
            .publishWith()
            .topic(destinationTopic)
            //.correlationData(correlationData.getBytes())
            .responseTopic(responseTopic)
            .payload(message.getBytes())
            .qos(MqttQos.AT_LEAST_ONCE)
            .send()
            .whenComplete((result, throwable) -> {
                if (throwable != null) {
                    logger.info("Error sending to '{}': {} - {}", destinationTopic, message, throwable.getMessage());
                } else {
                    logger.info("Message sent to '{}': {} - {}", destinationTopic, message, result);
                }
            });

When monitoring the messages on http://www.hivemq.com/demos/websocket-client/ and on a subscriber, messages are only received when the line with `correlationData()' is commented, as you can see above.

Both with or without that line, the application logs a successful sending, e.g. with correlation data enabled:

Message sent to 'server/test': testMessage - MqttQos2Result{publish=MqttPublish{topic=server/test, payload=11byte, qos=EXACTLY_ONCE, retain=false, responseTopic=server/test/reply, correlationData=6byte}, pubRec=MqttPubRec{packetIdentifier=1}}

Any idea why the additional correlationData seems to cause that they are not shown on the websocket test page, and are not received on any of the subscribers?

As an experiment, I used the paho 5 library instead of the HiveMQ library with the following code, but had exactly the same behavior and needed to disable the line to see messages passing:

    MqttProperties properties = new MqttProperties();
    //properties.setCorrelationData(correlationData.getBytes());
    MqttMessage mqttMessage = new MqttMessage();
    mqttMessage.setQos(1);
    mqttMessage.setPayload(message.getBytes());
    mqttMessage.setProperties(properties);
    try {
        mqttConfig.getClient().publish(destinationTopic, mqttMessage);
        logger.info("Message was sent to '{}': {}", destinationTopic, message);
    } catch (MqttException ex) {
        logger.error("Error sending to '{}': {} - {}", destinationTopic, message, ex.getMessage());
    }
Frank
  • 5,741
  • 4
  • 28
  • 49
  • 1
    The MQTT WebSocket client is only connecting to the broker with MQTT v3.1 what happens if you use a different MQTT v5 client as a subscriber? – hardillb Dec 02 '21 at 17:07
  • 1
    Also please edit the question to make it clear which broker you are using (since the hivemq tag could be either the client, broker or both) – hardillb Dec 02 '21 at 17:15
  • Thanks for your feedback. Broker is HiveMQ Cloud (added to the question). Good remark about the websocket client being v3.1, but when adding a subscriber to the Java-application itself, I only receive the messages when commenting the setCorrelationData-line. So this behaves the same way as the websocket client and other subscribers. – Frank Dec 02 '21 at 18:43
  • 1
    Follow-up test, does it work with mosquitto as the broker (just trying to narrow the search space, and you've changed everything else)? – hardillb Dec 02 '21 at 18:57
  • Didn't try yet with Mosquitto, but started HiveMQ in a Docker locally and that worked! So seems to be related to HiveMQ Cloud?! – Frank Dec 02 '21 at 21:27
  • 1
    Sounds like you need to raise a bug with HiveMQ about their Cloud offering. – hardillb Dec 02 '21 at 21:28
  • Indeed thinks so, same question and findings is also on their forum... – Frank Dec 02 '21 at 21:32
  • Confirmed: also works as expected with a Mosquitto on stackhero.io – Frank Dec 02 '21 at 21:49

1 Answers1

1

This behaviour has now been fixed by HiveMQ Cloud.

fraschbi
  • 533
  • 3
  • 6
  • While this might answer the question it's not really all that useful. Can you edit it to include a link to the issue or some other indication to explain what happened? – hardillb Dec 17 '21 at 15:45
  • There was an issue with our cloud free deployment. Issue was reported over our community forum: https://community.hivemq.com/t/how-to-use-the-correlationdata-messages-never-arrive/851/8 – fraschbi Dec 17 '21 at 23:50