auto_reconnect(3)

✨ NAME ✨

auto_reconnect β€” Automatic Reconnect πŸ¦‹

okay bestie, so picture this: your client is out here vibing with a connection, and then πŸ’€ the connection just... dips. and auto_reconnect is the feature that says "no babe, we're getting her back." the ability for the client library to reconnect automatically in the event of a connection failure was added in 1.1 β€” so if you're on anything older, that's giving ancient history, sorry not sorry. πŸ™ˆ

ngl, the connection lost callback is the other way to handle this, and it's genuinely iconic β€” it lets you have a flexible response to the loss of a connection, so basically almost any behaviour can be implemented in that way. but automatic reconnect does have the advantage of being a little simpler to use. main character energy: low effort, high reward. πŸ’…


🌸 how to turn it on 🌸

to switch on automatic reconnect, the connect options field automaticReconnect should be set to non-zero. that's it. that's the whole vibe check. ✨

the minimum and maximum times before the next connection attempt can also be set, the defaults being 1 and 60 seconds. yes, you can customise. we love options. πŸŽ€

and here's the little algorithm, and honestly? she's smart:

  • at each failure to reconnect, the retry interval is doubled until the maximum value is reached πŸ“ˆ
  • and there it stays until the connection is successfully re-established, whereupon it is reset πŸ”

so it backs off politely instead of spam-retrying like a chaotic ex. respect. πŸ‘‘

πŸ’œ when it actually works πŸ’œ

when a reconnection attempt is successful, the MQTTAsync_connected callback function is invoked, if set by calling MQTTAsync_setConnected. this allows the application to take any actions needed, such as amending subscriptions. so like, you get your lil callback and you can go "oh we're back? let me fix up my subscriptions real quick." πŸ’–

lowkey important: the callback only fires if you set it with MQTTAsync_setConnected. no setter, no callback. the way that works is just... the way that works. 😭

and that's the whole page, bestie, we ate and left no crumbs πŸ“

this lil love letter to reconnection came to you from the Paho Asynchronous MQTT C... library, dated Tue Jun 2 2026 00:00:00 β€” go forth and stay connected fr fr πŸŒˆπŸ’–

πŸ“¦ where this page comes from