Based on my experience with the MQTT client, follow these answers to your questions / queries. I hope this clarifies your concerns and helps you move forward.
1) How can I get my client to work in the background?
- You cannot keep your MQTT client running in the background, because Apple does not allow any application to continue to run in the background. Although, if you override it, it is not guaranteed that your application will run in the background. You can learn more about background execution support in Apple documentation .
- Also refer to the issue posted on github for this structure.
2) What will happen after rebooting the device? How can I automatically connect to the server after rebooting the device?
- Every time your application starts executing, you need to connect to your server using the MQTT client environment; there is no automatic connection mechanism in the MQTT-client environment. I suggest writing init your connection in a specific controller, which is executed immediately after starting your application, except for the same as
AppDelegate
3) Suppose that I received an error message when connecting to the server. Will this library try to connect in a loop? how many times did he try? or do i need to manage this on my own?
If your MQTT client cannot connect to your server, you need to process it yourself, the library does not automatically try to connect, as indicated in the previous answer. I wrote an example code as shown below. Use NSTimer
to automatically connect to the server.
[self.mqttSession connectToHost:MQTT_HOST port:MQTT_PORT usingSSL:NO connectHandler:^(NSError *error) { if(error) {
4) The same 3 script for subscribing to a topic?
- If your broker server has a configuration to track an existing subscription for individual users / clients, you do not need to subscribe every time.
Otherwise, each time you need to subscribe to this topic with a successful connection. Use the following MQTTSessionDelegate
method to subscribe.
- (void)connected:(MQTTSession *)session
Happy coding :)
source share