Skip to content

After disconnect, client tries repeatedly reconnecting indefinitely with no reason other than "CLIENT_ERROR"

0

I use iot for a chat application and only more recently started having an issue where when a client times out an disconnects, for instance if the user switches tabs and chrome throttles the connection and/or it goes beyond the keep-alive, the client will start trying to reconnect, which is normal behavior.

However unlike before, now when then client tries to reconnect it fails and is immediately disconnected, thus starting an endless repeating cycle of connect -> disconnect -> reconnect -> repeat.

I've checked the aws iot logs and all I can see is "CLIENT_ERROR" with no other reason given for the disconnect.

I've found a github issue that looks very familiar here: https://github.com/mqttjs/MQTT.js/issues/781

The way my application is architected, clients can have up to 10-15 subscriptions active at time of disconnect.

Is it true there is a limit of 8 subscriptions in a reconnect? If so, how can I mitigate this? If not, what could be the issue or how could I get more information on why it's failing to reestablish a connection?

Unsure if it's something on my end (using mqtt.js), or a limitation with iot I wasn't aware of. Need to mitigate somehow!

Help appreciated, thanks.

https://imgur.com/a/Y8PzerV

asked 3 years ago1.2K views
1 Answer
0
Accepted Answer

Hi,

Yes, there is a limitation of 8 subscriptions per subscribe request, so that could be the problem. You mentioned log files, what verbosity is set? You may be able to see more details by changing the logging level to DEBUG, but it's likely if your are attempting >8 subscriptions in a single call that this is the reason.

The most straightforward way would be to change your code to iterate through your topic subscriptions and make separate calls limited to 8 topics. Since it works for an initial session, is the logic different during reconnect?

Another approach would be to use persistent sessions. This would allow your initial connection to subscribe to the topic list (with the constraint of 8 per request), but then when you do a reconnect IoT Core will automatically reinstate all subscriptions.

When a client connects to the message broker using a persistent session, the message broker saves all subscriptions that the client makes during the connection. When the client disconnects, the message broker stores unacknowledged QoS 1 messages and new QoS 1 messages published to topics to which the client is subscribed. ... When the client reconnects to its persistent session, all subscriptions are reinstated and all stored messages are sent to the client at a maximum rate of 10 messages per second.

Also, the AWS IoT Device SDKs have a lot of logic built-in (plus MQTT 5 support!). If it's possible, using the JavaScript SDK might help with this.

Please respond (and/or accept if this resolves your question) and if possible, share some code snippets for the initial connect and reconnect logic in your application.

AWS
answered 3 years ago
EXPERT
reviewed 2 years ago
  • Hey Gavin, thank you for the reply. I was able to confirm via iot debug logs that the subscribe requests that were failing had e.g. 14 topics in the batch :) I just did some initial testing setting clean: false with the mqtt client and it seems promising. Still going to do further testing but it does appear that was the issue I was hitting!

    At some point I would like to upgrade to mqtt 5 but haven't had the chance to look into it yet.

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.