Retrieving current state of device shadow on first subscribe

0

Hi, in experimenting with the classic device shadow I have found I can publish to topics like $aws/things/DEVICE_NAME/shadow/update and correctly see the updates as expected if I first subscribed to topics like .../shadow/update/delta or ../shadow/update/documents topics, but I have not been able to figure out how to see the already existing document at the moment I call subscribe. I tried subscribing to a number of different topics but in app code and simply in the MQTT test client but none of the subscriptions return any data until a subsequent publish is done.

Is there something obvious I am missing? Or is there maybe a different method to get the already existing data of the device shadow at the time of subscription? Thanks.

asked 2 years ago225 views
1 Answer
1

Hi,

Thanks for reaching out. I understand that you would like to retrieve the existing shadow document of a thing. This is certainly possible but I would like to confirm that a subsequent publish is required to get the existing shadow document. This is discussed here in our documentation

First, there would be a need to subscribe to the topics ShadowTopicPrefix/get/accepted and ShadowTopicPrefix/get/rejected as this is where the shadow document will be sent(or if the shadow cannot be retrieved).

Once subscribed to the above topics, there would be a need to publish an empty message to the ShadowTopicPrefix/get topic. The current shadow will then be returned to the ShadowTopicPrefix/get/accepted topic.

I've just tried this out and it does work as expected. I recommend trying it out in the MQTT test client in the AWS IoT console first. There is currently no topic which will return the existing shadow after subscribing without any subsequent publish.

An alternative to the above method would be using GetThingShadow API -> https://docs.aws.amazon.com/iot/latest/apireference/API_iotdata_GetThingShadow.html. You can see the SDK equivalent of this API at the bottom of the documentation, for example, here is the link for the python boto3 SDK

I would recommend the first method since you are already using MQTT to communicate with IoT but just letting you know that the second method exists as well.

Please let us know if you may have any other questions.

AWS
SUPPORT ENGINEER
Ryan_A
answered 2 years ago
  • Thanks a lot! I figured I might need to publish an empty one when just doing a subscribe but hoped maybe a missed something. Is there an equivalent react-native javascript library that calls GetThingShadow or do you recommend possibly making the API call directly when the app starts?

    Also just curious what the difference between publishing to and subscribing to the /get and /get/accepted topics vs /update and /updated/accepted

  • Hi,

    Thanks for your response. I checked our docs and we have documentation on how you can use the Javascript SDK in React Native -> https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-started-react-native.html.

    A publish to the /update topic means that you are trying to update the shadow document. If there is a corresponding message to the /updated/accepted this means that the update has been accepted -> https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html#update-pub-sub-topic

    On the other hand, publishing an empty message to /get only retrieves the current shadow without any updates. As mentioned earlier, the current shadow state will be published to the /get/accepted topic.

    Hope this helps! Let us know if anything is unclear.

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.

Guidelines for Answering Questions