0

I am using the current version of Paho MQTT android client,( compile org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0) and I am trying to get the message ID from every message received from the messageArrived() callback. This is how I am doing it.

   @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            String plainMessage = new String(message.getPayload());

              int messageID= new Integer(message.getId());
              System.out.println(messageID);

        }

Update With a QoS of 2

The message ID returns Zero anytime the message arrives or the method messageArrived is called.

Please does anybody has any ideas as to how to resolve this?

George
  • 1,086
  • 14
  • 48

1 Answers1

2

I am not familiar with Paho, but supposing that "message id" is the same thing as "packet identifier" being talked about in MQTT spec: it's not an unique numbering sequence for all of your messages. It's just two bytes (so it probably is the same thing because Paho is using the smallest primitive type capable of holding two bytes unsigned). It's meant to match multiple messages in flight during the multi stage handshake of QoS>0. So my theory is this: whatever MQTT broker you are attaching to, it's using this limited sequence sparsely. It may show nonzero numbers only when there is multiple messages in flight. Which you should be capable of testing simply, if i read Paho javadoc right: keep firing messages but hold returning from messageArrived.

Try it and let me know. I'm speculating a little bit. If i speculate some more, that you wanted to use this message id as your application-level unique identifier of all your messages: this is not the right tool. You must provide your own sequence on application level.

Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
  • Very well, but does that means I hv to generate my own message ID and add it to the message payload using probably `json`. Assuming I use that, don't you think it will slow the publishing function. What do you think? – George Jan 25 '17 at 13:55
  • What kind of slowing are you afraid of? I cannot imagine any measurable slowing by adding a single Int64 to your payload. The encoding competely depends on your current payload format. – Pavel Zdenek Jan 25 '17 at 14:48
  • If I add an int64 to the payload, this will mean that the recipient may receive duplicate messageIDs for two different messages. So I intend fetching the unique message ID from the local database(of the sender since every message will be saved in the senders database). This is how I plan to do it buh I think it might slow the message wen publishing it. – George Jan 25 '17 at 17:18
  • "_duplicate messageIDs for two different messages_" how come, for one client? I mean monotonic sequence, not some random numbers. – Pavel Zdenek Jan 26 '17 at 07:27
  • Oohok.....I have arrived at `System.getCurrentTimemills()`, which is added to the message payload. Its monotonic, so I guess I can use it. – George Jan 26 '17 at 12:15
  • Please kindly check this link if you have any idea why its working that way, http://stackoverflow.com/q/41874127/2595059 – George Jan 26 '17 at 13:30
  • @George the link is broken – DOUBL3P Feb 22 '19 at 14:38
  • @PavelZdenek what was your solution at the end? – DOUBL3P Feb 22 '19 at 14:39
  • @George i was not looking for a solution, i proposed one. I don't know if it was used or something else. – Pavel Zdenek Feb 23 '19 at 15:53