MQTT-Client-Framework runs in the background

I am trying to connect to our push server through the MQTT-Client-Framework .

You have no complications to connect to the server and with a few lines of code I can connect to the server and subscribe to the topic.

but I have a few questions that I could not find a direct answer for them.

1) How can I get my client to work in the background?

2) What will happen after rebooting the device? How can I automatically connect to the server after rebooting the device?

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?

4) The same 3 script for subscribing to a topic?

+5
source share
3 answers

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) { // Application fail to connect to server, write your code to auto connect here } }]; 

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 :)

+2
source

1) Project-> Capabilities-> Background Modes. There are several options for running your application in the background.

2) Generally speaking, MQTT will not be disconnected from the server if your application is allowed to run in the background, but I think that you better check the connection and possibly reconnect MQTT to your server when the application becomes active again.

AppDelegate-> - (void)applicationDidBecomeActive:(UIApplication *)application;

3) Unfortunately, yes. And you need to control yourself.

4) I can not help.

+1
source

For your first question:

Details on how to work in the background on iOS can be found here . This link also lists the actions that Apple allows you to run in the background, if your application does not meet these criteria, then it will most likely exit the Apple app store.

The list also shows which UIBackgroundModes put in your Info.plist to indicate that your application needs background access.

The other 3 I can not help with

0
source

Source: https://habr.com/ru/post/1245140/


All Articles