How to Automatically subscribe to topic when reconnect

hello, i am new in mqtt, i am in a middle of developing a android project that required a automatic reconnect to mqtt broker, and when reconnect, i want to automatically re-subcribe the topic.

i want to ask, what is the best practice to reconnect into a broker but when reconnect its also subscribe into topic

Hello @Blazerstrom

Greetings and welcome to the HiveMQ Community Forum! We’re delighted to see your interest in MQTT and HiveMQ.

May we kindly inquire whether you are currently using the HiveMQ Java client library? If you are, we have provided examples on how to implement reconnection in our Git repository. You can access these examples here.

Moreover, if your aim is to have clients retain all subscribed topics after a reconnection, we recommend utilizing persistent sessions. Our blog post on persistent sessions provides valuable insights, and you can access it here. For implementation with the Mqtt 5 client, you can use cleanStart(false), and for Mqtt 3 client, you can use cleanSession(false).

Feel free to explore these resources, and if you have any further questions or require assistance, feel free to respond here.

Sheetal from the HiveMQ Team

yes, i am using HiveMq Java client library , ok thanks for the info @SShet