Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MQTT-Client-Framework stay running at background

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?

like image 825
mehdok Avatar asked Mar 16 '16 08:03

mehdok


2 Answers

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

like image 149
Dipen Panchasara Avatar answered Oct 22 '22 13:10

Dipen Panchasara


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.

like image 1
jojoT Avatar answered Oct 22 '22 13:10

jojoT