简体   繁体   中英

Mqtt Client: get Retained Message after Subscribing

I am using the latest Paho version via Maven .

<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
    <version>1.2.2</version>
</dependency>

I create client using

MqttClient client = new MqttClient("tcp://localhost", MqttClient.generateClientId());
MqttConnectOptions options = new MqttConnectOptions();
options.setMaxInflight(1000);
options.setAutomaticReconnect(true);

Then I subscribe to a topic as follows:

    client.setCallback(new Callback());
    client.connect();
    client.subscribe(topic);

Another mqtt client publishes a message on that topic with

MqttMessage message = new MqttMessage(byteStream);
message.setRetained(true);

With the retain flag I would expect that as soon as I subscribe, my callback is invoked. Unfortunately, the subscription callback is NOT called if the message is sent before the subscription is executed. How do I get the retained value?

I think you are using QOS=0. It is possible a retained message not saved with QOS=0 and retained_flag=true.

More details:

Reference link:

SECTION (3.3.1.3 RETAIN):
If the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a Server, the Server MUST store the Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its topic name [MQTT-3.3.1-5]. When a new subscription is established, the last retained message, if any, on each matching topic name MUST be sent to the subscriber [MQTT-3.3.1-6].

If the Server receives a QoS 0 message with the RETAIN flag set to 1 it MUST discard any message previously retained for that topic. It SHOULD store the new QoS 0 message as the new retained message for that topic, but MAY choose to discard it at any time - if this happens there will be no retained message for that topic [MQTT-3.3.1-7]. See Section 4.1 for more information on storing state.

Summary:
You can use QOS>0 to solve your problem.

Unfortunately, the subscription callback is NOT called if the message is sent before the subscription is executed. How do I get the retained value?

In this case, the publisher (one client) sends out message, immediately disconnects from MQTT broker (server), then the subscriber (another client) connects to the server with the same topic, Without last will message , it is not possible that the published message will be delivered to your subscriber.

There would be options in paho to enable last will message by setting:

  • will flag
  • will retain flag
  • will topic
  • will message (will payload)
  • retain flag in PUBLISH control packet (not the same as will retain flag)

Set up all of them when the publisher sends out the message with a topic, the sent message will be retained on MQTT broker even after the publisher closes network connection. At a later time when any subscriber (another client) connects to the broker with the same topic, the retained message will be sent from the broker to the subscriber.

Also please note that QoS field of PUBLISH control packet is for ensuring delivery is complete (at different level) ONLY between MQTT publisher and MQTT broker(server), NOT between MQTT publisher and subscriber (the 2 clients).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM