Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS MQTT do not receive message after reconnection

I am developing an iOS application using MQTT-Client-Framework. The app successfully communicates with the server at the first launch.

But after I turn it to background and get back to foreground, I no longer receive any message from the MQTT server.

Everytime the app resign active and become active, I will do the reconnection with a new Mqtt object (different client ID):

func applicationWillResignActive(_ application: UIApplication) {
    //Unsubscribe topic, disconnect the mqtt client, and close the client
}
func applicationDidBecomeActive(_ application: UIApplication) {
    //Renew mqtt client, set delegate, reconnect to server and subscribe topic
}

However, I no longer receive any message from server after the reconnection. I checked the client status, and it is connected. How can I make the application receive message after reconnection?

like image 612
Jimmy Pun Avatar asked Oct 18 '22 18:10

Jimmy Pun


1 Answers

Fixed myself by changing the clean session flag to false Do not need to unsubscribe the topic

like image 188
Jimmy Pun Avatar answered Oct 21 '22 04:10

Jimmy Pun