I have a few devices that produce data over MQTT sent to GCP IoT core. I want to monitor if the device is currently connected or disconnected from the MQTT broker. This was already answered elsewhere on SO and this is the approach I'm currently using: monitor Stackdriver logs for CONNECT/DISCONNECT messages and publish them to a separate Pub/Sub topic. This topic is then read by a Firebase Function to update my device online/offline field.
This works in theory, but due to the JWT expiration field, I am forced to re-connect my devices every time the token is expired, causing a lot of spurious online/offline triggers. Is there a way perhaps refresh the token while keeping the connection? Or a better approach to simply monitor connectivity of a device?
I can probably use the hearbeat event and only consider device being offline if no hearbeat was received within N amount of minutes, or even periodically sending some kind of ping command to the device. Although, it would be nice to simply know if device is currently connected or not.