Search code examples
amazon-web-servicesaws-iotaws-iot-core

How to check if device is offline in AWS IoT Core


What is the best practice to check if AWS IoT Core thing is still offline?

Being able to query the state of an AWS IoT thing will for many be an essential part of their application. Lucky AWS has a best practise on how to get lifecycle events here: https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html

In the documentation there is a section describing how to deal with lifecycle events an here they mention that after receiving a disconnect life cycle event you should wait some time before checking if the device is still offline.

When that message becomes available and is processed (by Lambda or another service), you can first check if the device is still offline before taking further action.

The question here is how do i check if the device is still offline?

The suggested infrastructure in the documentation is a IoT Rule subscribed to topic '$aws/events/presence/disconnected/clientId' will create a message on a SQS queue. The SQS queue will delay the message using the deliveryDelay attribute, before a lambda function will consume the message. It is in this lambda function that we should check if the device is still offline.

I could simply implement a dynamodb table with a device state and a timestamp which will be updated every time there is a connect or disconnect event without delay. But there must be a smarter way to determine if the device is still offline?


Solution

  • You already have the answer. Once the disconnect event is fired, you will assume the client is staying offline unless IoT Core sends update for the same client on the topic '$aws/events/presence/connected/clientId'.

    Obviously you need to subscribe both topics.

    Thing Indexing is a bad idea in most cases if you think about why you need IoT from beginning. It's only good for Amazon but won't give you any value.