- Newest
- Most votes
- Most comments
Hey.
It sounds like your Persistent session has expired. The default session time is one hour so unless you have changed that, this is the reason you don't get the messages after a long disconnect. As per documentation: "If a client doesn't reconnect to its persistent session before it expires, the session ends and its stored messages are discarded. " https://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html#mqtt-persistent-sessions
Changing the session time should solve your problem.
AWS IoT assigns a unique session identifier to each client connection. This identifier is generated at the beginning of a new MQTT session, and it remains the same throughout the lifecycle of that session.
You have mentioned that your application is not receiving events after waking up from sleep and re-establishing the connection. This might happen because the session has been considered "expired" on AWS IoT end. When a new connection request comes in, AWS IoT sees it as a new session, thus assigns a new session identifier. The old session with all its subscriptions is discarded and thus your application no longer receives messages for those topics.
AWS IoT supports the MQTT protocol, which includes the notion of persistent sessions (also called "clean sessions" in MQTT terminology). When a client connects to AWS IoT using MQTT, it can specify the cleanSession flag in the CONNECT message.
- If cleanSession=true, the session lasts as long as the network connection. When the client disconnects, AWS IoT does not store any session data.
- If cleanSession=false, the session is persistent. The session stays alive for a period of time after the network connection is closed. AWS IoT stores the session data, which includes subscriptions and unacknowledged messages.
In AWS IoT, the session expiration period is not configurable and the duration is not publicly documented, but it's not infinite. If the connection is closed and remains closed for a prolonged period of time, AWS IoT may decide to close the session. It seems like this is what is happening in your case when the application sleeps overnight.
To mitigate this, consider the following:
- Resubscribe after reconnecting: Update your application to resubscribe to the necessary topics each time it reconnects to AWS IoT. This ensures that the application continues to receive messages even if the session was closed and a new session was started.
- Keep-alive Messages: MQTT has a keep-alive mechanism where the client sends PINGREQ messages to keep the connection alive. Adjust your keep-alive interval to ensure the session is not closed due to inactivity.
- Session State Management: If you're relying on the sessionIdentifier for maintaining state in your application, consider managing the state within your application instead.
Remember to review your MQTT client settings and ensure you have correctly configured your client for persistent sessions (cleanSession=false). If you continue to face issues, it might be helpful to use AWS IoT logging to debug your MQTT sessions and understand what is happening to your connections and sessions over time.
Thanks, yes I am using persistent sessions, and the reason I switched to using that versus managing resubscribing myself is because my application has pages that subscribe to 20+ topics and aws has a limit of maximum 8 topics in a subscribe request. It just became a pain when using persistent session is simple. All of this would be solved if AWS exposed "sessionIdentifier" outside of reserved topics or somehow passed it to clients upon connection. That value is all I need on the client.
Relevant content
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 3 months ago
Hey thanks, yeah sounds like that's the issue! However in the linked doc I don't see anything about how to change the value of the persistent session expiration. The only thing I see it mentioning is that the default is one hour. Is there a way to configure it to be longer or even indefinite? Where is this configured? I don't see that as a client-side connection property (mqtt.js docs)
"In MQTT 3, the default value of persistent sessions expiration time is an hour, and this applies to all the sessions in the account."
ok nvm found the page to request an increase. Only thing is it still has the same problem hypothetically even at the max of 7 days. Ideally I could store it indefinitely, but I'm thinking maybe I need a mechanism on the client to somehow detect if upon reconnect it is a new persistent session. It'd be nice if that sessionIdentifier were somehow transferred to the client upon connection e.g. via connect event.