8

I'm trying to connect to our push server via MQTT-Client-Framework.

There is no complication for connecting to server and with a few line of code i can connect to server and subscribe to topic.

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

1) How can I keep my client running at background?

2) What happen after device restart? how can I automatically connect to server after device restart?

3)Suppose I got an error during connecting to server. Will this library trying to connect in a loop? how many times it try? or I need to manage this myself?

4) The same 3 scenario for subscribing to topic?

josip04
  • 224
  • 3
  • 13
mehdok
  • 1,499
  • 4
  • 29
  • 54

4 Answers4

4

Based on my experience on MQTT-client framework following are the answers to your questions/queries. I hope it clarifies your concerns and helps you to move ahead.

1) How can I keep my client running at background?

  • You can not keep your MQTT client running in background, as Apple doesn't allow any application to keep running for long time in background. Though if you override its not guaranteed your application will keep running in background. You can read more about background execution support in apple documentation.
  • Also refer issue posted on github for given framework.

2) What happen after device restart? how can I automatically connect to server after device restart?

  • Each time your app begin execution you need to connect to your server using MQTT client framework there is no auto connect mechanism available in MQTT-client framework. I suggest to write init your connection in specific controller which executes immediately after your app launch except same as AppDelegate

3) Suppose I got an error during connecting to server. Will this library trying to connect in a loop? how many times it try? or I need to manage this myself?

  • If your MQTT-client fails to connect your server, you need to handle it yourself, library doesn't try to auto connect as mentioned in previous answer. I have written sample code as below. Use NSTimer for auto connect to 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 scenario for subscribing to topic?

  • If your broker server has configuration to track your existing subscription for individual users/client then you don't need to subscribe each time.
  • Otherwise each time you need to subscribe to same topic on successful connection. Use following MQTTSessionDelegate method to subscribe.

    - (void)connected:(MQTTSession *)session
    

Happy coding :)

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • This is almost all the answer, and based on it i can not stay connected yo my server in background, BUT massengers like telegram, what they do to stay connected to their server? – mehdok Mar 25 '16 at 07:16
  • 4
    They don't stay connect in background, they send `APNS` Push notification for each new message, which notify user about new message. Both the thing work parallel, They send message via MQTT as well as Notification service. – Dipen Panchasara Mar 25 '16 at 07:27
  • tnx alot, this is all i need. but is there any hack to stay connected in background? NOTE: i don't mind if apple wont approve app for store, the app has inter organization use. – mehdok Mar 25 '16 at 07:36
  • There is no specific hack but you can follow this approach as mentioned in [post](http://stackoverflow.com/questions/9738488/run-app-for-more-than-10-minutes-in-background). – Dipen Panchasara Mar 25 '16 at 07:39
1

1)Project->Capabilities->Background Modes. There has some options for allowing your app to run at background.

2)Generally speaking, MQTT will not be disconnected to the server if your app is allowed to run at background, but i think you would better check up the connection and maybe re-connect MQTT to your server when the app become active again.

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

3)Unfortunately, yes, it will. And you have to manage yourself.

4)I can't help.

jojoT
  • 153
  • 15
  • The Background mode works for a limited time ( 3 to 10 min) i need to stay connected to the server as long as possible . – mehdok Mar 25 '16 at 05:33
0

For your first question:

Details on how to run in the background on iOS can be found here. This link also lists the actions that Apple allows to run in the background, if your app does not meet those criteria then it is likely to get thrown out of the app store by Apple.

The list also shows which UIBackgroundModes to place in your Info.plist to flag that your app needs background access.

The other 3 I can't help with

hardillb
  • 54,545
  • 11
  • 67
  • 105
0

We all know that Apple doesn't allow app service to run in the background, so MQTT will be disconnected in the background mode.

Now do one thing use better frameworks for MQTT like this in this framework you will get auto-reconnect and callbacks and many things. So When you receive a call back that the MQTT is connected, immediately subscribe to all the topics that you have.

And if you want to get all missed messages then you need to change the MQTT configuration to like 'clean = false'.