Skip to content

How to get the Retained Messages in AWS IoT Core?

0

Hello, I am using the AWS IoT Device SDK for Embedded C for one of my use case taken from below link. https://docs.aws.amazon.com/iot/latest/developerguide/iot-embedded-c-sdk.html And here are my points captured from my test. Case 1: Whenever i run the application (with retain flag is 0) i can see the data received in the MQTT test client and wireshark application. Below are my policy details and wireshark log. Enter image description here Enter image description here Enter image description here Case 2: Whenever i run the application (with retain flag is 1) i can not see the data received in the MQTT test client and wireshark application. Below are my policy details and wireshark log. Enter image description here Enter image description here Enter image description here I have enabled the Retained Messages from AWS IoT Core like mentioned below but am not receiving any retained messages from the application. And i followed the link mentioned below for this test. https://aws.amazon.com/blogs/iot/getting-started-mqtt-retained-messages-aws-iot-core/ Enter image description here After this i made changes in the policy that i specifically mentioned the client id and topic also as mentioned in the above link. you’ll need to match the topic filter exactly without the use of any wildcards (i.e. #,)* But still am unable to see the retained messages in the aws iot core. So, please help on this to capture the retained messages in aws iot core.

Thanks & Regards Manoj

asked a year ago415 views

4 Answers
0

To get the Retained Messages in AWS IoT Core, you need to follow these steps:

  1. Ensure you have the correct permissions: Your AWS IoT Core policy should include the iot:GetRetainedMessage action for the specific topic you're interested in.

  2. Use the appropriate API: AWS IoT Core provides two main methods to access retained messages:

a. ListRetainedMessages: This action lists summary information about the retained messages stored for your account. It returns only the topic names of the retained messages, not the message payloads.

b. GetRetainedMessage: This action retrieves the details of a single retained message for a specified topic, including the message payload.

  1. For your specific case using the AWS IoT Device SDK for Embedded C, make sure you're using the correct MQTT API calls to publish and subscribe to retained messages.

  2. When publishing a retained message, set the RETAIN flag to 1 (as you've done in Case 2).

  3. To receive retained messages, subscribe to the appropriate topic. When you subscribe, you should receive any existing retained messages for that topic immediately.

  4. If you're not seeing the retained messages in the MQTT test client, double-check that you're subscribed to the correct topic and that your policy allows you to receive messages on that topic.

  5. Note that publishing retained messages incurs additional charges. Each retained message publish is metered and charged as both a regular PUBLISH message and an additional RETAINED message charge for storing it.

  6. For MQTT 3, retained messages do not expire and are stored until replaced or deleted. For MQTT 5, retained messages expire after the message expiry interval you set.

If you're still not receiving retained messages after following these steps, verify that the retained message was successfully published and stored by using the AWS IoT Core console or the AWS CLI to list and get retained messages. This can help you determine if the issue is with publishing or receiving the retained messages.

Remember to check your AWS IoT Core settings, ensure your policy permissions are correct, and verify that you're using the API correctly in your application.
Sources
GetRetainedMessage - AWS IoT
AWS IoT Core policy actions - AWS IoT Core
Retained message policy examples - AWS IoT Core
ListRetainedMessages - AWS IoT
Pricing for IoT Core MQTT Retained Messages | AWS re:Post

answered a year ago

0

Thanks for the detailed breakdown — this can definitely get confusing when you’re dealing with retained messages in MQTT.

Here are a few key things to double-check:

Topic matching must be exact. Retained messages in AWS IoT Core are only delivered if the subscription topic exactly matches the retained message's topic — no wildcards (# or +). It has to be a perfect string match.

Client must subscribe after the retained message is published. If your device connects before the retained message is published (and isn't subscribed to the topic), it won’t receive anything. Try publishing the retained message first, then reconnect and re-subscribe to test.

Retain flag matters. It looks like you did set the retain flag to 1 when publishing — that's good. But make sure the payload isn't empty, otherwise it could actually delete the retained message (per MQTT spec).

Permissions look mostly fine, but just in case, add:

json Copy Edit "Action": [ "iot:Publish", "iot:Receive", "iot:Subscribe", "iot:Connect", "iot:GetRetainedMessage", "iot:ListRetainedMessages" ] ...to your policy to be fully covered for retained message testing.

Use AWS CLI to confirm storage: Try:

bash Copy Edit aws iot list-retained-messages aws iot get-retained-message --topic <your/topic> That way you know for sure if the message is stored.

answered a year ago

0

Hello,

I mentioned everything in my previous message. I followed all the steps from the link below and replicated the example for retained messages, and it's working.

Enter image description here

https://aws.amazon.com/blogs/iot/getting-started-mqtt-retained-messages-aws-iot-core/

After making changes to my policy (i did not mentioned any wildcard entries in policy) and testing my application (i had set the retain flag to 1) according to the provided links, I noticed that no messages were being received from the device on the subscribed topic in the MQTT test client.

Enter image description here

https://docs.aws.amazon.com/iot/latest/developerguide/retained-message-policy-examples.html

Please review this and let me know exactly what I'm missing.

Regards Manoj

answered a year ago

0

Hi Manoj,

You're on the right track with enabling retained messages and setting the retain flag to 1, but here are a few critical troubleshooting tips specific to AWS IoT Core + MQTT retained messages that may help clarify what's going wrong:

Confirm Retained Message Was Actually Stored Use the AWS CLI to verify whether the retained message is stored:

bash Copy Edit aws iot list-retained-messages aws iot get-retained-message --topic <your/topic> If nothing shows up, the message might not have been retained properly—possibly due to an empty payload or incorrect topic structure.

MQTT Rules for Retained Messages in AWS IoT Core No Wildcards: Subscriptions must exactly match the retained message topic (e.g., devices/device123/data) — wildcards like # or + will not work.

Retained = Stored Only on New Publish: If your app connects before the retained message is published, it won't receive it retroactively. The retained message is only pushed at the time of subscription, assuming the message already exists.

Empty Payload = Deletion: Per MQTT spec, publishing a retained message with an empty payload deletes the retained message for that topic.

Policy Permissions Check Make sure your IoT policy includes all relevant actions:

json Copy Edit "Action": [ "iot:Connect", "iot:Publish", "iot:Receive", "iot:Subscribe", "iot:GetRetainedMessage", "iot:ListRetainedMessages" ] Also, ensure the resource ARNs are specific (no wildcards like * in subscriptions).

Debugging Tips Try using the MQTT Test Client in AWS IoT Core to manually publish a retained message, then connect your device and subscribe. This helps rule out SDK-related issues.

If you're using Wireshark, look for the RETAIN flag in the MQTT publish packets — that confirms whether it's being set correctly at the protocol level.

Let me know your results from get-retained-message, and we can narrow this down further.

—TheQuietBuilder (AWS Community Member)

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.