Skip to content

How to load retained messages after connecting?

0

Hello, good afternoon!

I am having problems with retained messages in MQTT AWS IoT Core. When I send messages with the “Retained” flag enabled, I see that they appear in the retained messages menu of the Amazon console.

However, when I connect through an MQTT client (e.g., MQTT Explorer), I cannot see these messages. I have sent retained messages in three different ways:

  • node-red mqtt-in node
  • publishing by activating “Retained” in MQTT Explorer
  • publishing with the Python Paho library

If I am subscribed, I can see these messages without any problems. But when I close the connection and reconnect, these messages no longer appear. The same thing happens if I send “Retained” messages without being connected to a client to view them and then connect to view the retained messages.

In other brokers, I can see that they are marked as “Retained” when I connect through MQTT Explorer, but when I use AWS IoT Core, they do not appear.

Retained flag

A list of retained messages does appear in the Amazon console, but I cannot see these messages as soon as I connect to the broker.

Retained messages

What could be happening? Thank you!

asked a year ago206 views

2 Answers
0

To access retained messages in AWS IoT Core after connecting, you need to understand a few key concepts about how AWS IoT Core handles retained messages.

When you publish MQTT messages with the RETAIN flag set, AWS IoT Core stores these messages, and they appear in the retained messages menu of the Amazon console as you've observed. However, unlike some other MQTT brokers, AWS IoT Core doesn't automatically send retained messages to clients upon connection and subscription.

To retrieve retained messages in AWS IoT Core, you have two options:

  1. Use persistent sessions: Set the clean_session parameter to False when connecting your MQTT client. With a persistent session, the message broker stores messages while the device is disconnected and delivers them when the device reconnects. This approach works for messages sent while you were disconnected but had an active subscription.

  2. Explicitly retrieve retained messages: Since AWS IoT Core doesn't automatically send retained messages upon connection, you need to use the AWS IoT API to retrieve them:

  • Use ListRetainedMessages to get a list of topic names for retained messages
  • Use GetRetainedMessage with the specific topic name to retrieve the message payload

Note that using these API calls requires proper permissions in your AWS IoT policy. You'll need to ensure your policy includes permissions to access the ListRetainedMessages and GetRetainedMessage actions.

The behavior you're experiencing is expected with AWS IoT Core, as it handles retained messages differently from some other MQTT brokers that might automatically send retained messages upon subscription.
Sources
Retained message policy examples - AWS IoT Core
ListRetainedMessages - AWS IoT
list_retained_messages - Boto3 1.37.31 documentation
Tutorial: Connecting a device to AWS IoT Core by using the AWS IoT Device SDK - AWS IoT Core

answered a year ago

0

Thank you for your reply. How can I use the Rest HTTP API to retrieve retained messages?

There is hardly any information in the documentation, and it does not indicate how I should authenticate myself.

There is only a final endpoint, but no example of the parameters I need to authenticate myself.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.