0

I am using paho library Classes for Mqtt Connections org.eclipse.paho.client.mqttv3.MqttClient. (not MqttAsyncClient)

In my case when I publish using

mqttClient.publish(uid + "/p", new MqttMessage(payload.toString().getBytes()));

This method does the task for me but doesn't return anything so I can't check the latency between publish and pubAck.

To get the latency I use the following instead of directly calling publish function of mqttClient.

    public long publish(JsonObject payload , String uid, int qos) {
    try {
        MqttTopic topic = mqttClient.getTopic(uid + "/p");
        MqttMessage message = new MqttMessage(payload.toString().getBytes());
        message.setQos(qos);
        message.setRetained(true);
        long publishTime = System.currentTimeMillis();            
        MqttDeliveryToken token = topic.publish(message);           
        token.waitForCompletion(10000);
        long pubCompleted = System.currentTimeMillis();
        if (token.getResponse() != null && token.getResponse() instanceof MqttPubAck) {
            return pubCompleted-publishTime;
        }
        return -1;
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}

This gets the work done, but I am not sure whether this is the right approach or not. Please let me know in case there is some other way to to do this.

Nilu
  • 262
  • 1
  • 8
  • What do you think you need this information for? It only tells you how long it took to push the message to the broker, it has no bearing on the time it may take to reach a subscriber – hardillb Dec 19 '16 at 17:49
  • I was given a task to calculate the latencies for different types for payloads used in the organisation. Publish to PubAck, publish to payload received etc. – Nilu Dec 19 '16 at 17:59
  • I'd be more likely to use something like wireshark and track this out of band at the network level – hardillb Dec 19 '16 at 18:02
  • This is supposed to be a part of AutomationFramework and get the aggregated latency reports over the time – Nilu Dec 19 '16 at 18:04
  • I think you're likely to find the latency is predominantly a function of message size and network speed. Also as mentioned earlier, it's only between the publisher and the broker, not the end to end to a subscriber. I'm still struggling to see any real value in this metric – hardillb Dec 19 '16 at 20:39

0 Answers0