iOS Question Can MQTT (or a TCP Socket Connection) run in the background

aminoacid

Active Member
Licensed User
Longtime User
Is there any way to let MQTT function in the background? I observed that when the phone goes to sleep, the MQTT connection disconnects. It does recover somewhat gracefully when the phone wakes up, but I need the connection to be maintained and data published (and possibly received) while in the background. Has anyone had success with this? Since MQTT is essentially using a TCP socket connection, my question can be extend to TCP connections.

I know that you can keep background file transfers and location updates going with:

#PlistExtra: <key>UIBackgroundModes</key><array><string>location</string></array>
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>

But I could not find anything similar for TCP socket connections.
 

aminoacid

Active Member
Licensed User
Longtime User
Yes, I figured that would be the case. Thanks for the confirmation. I wanted to be able to send (publish) real-time location updates via MQTT even when the App is in the background. I guess my only other option is to prevent the phone from going to sleep.
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
Push notification depends from Google or thirty part supplies.
No one can guarantee that the service will still be active within 10 years.
Mqtt with own broker will works forever !

My app will no published into Apple store, is there any way to use mqtt into a background task?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Push notification depends from Google or thirty part supplies.
No one can guarantee that the service will still be active within 10 years.
As I see it, this is not a good reason to implement your own solution.
1. You can use push notifications without Firebase which means that it only depends on Apple APN service. I recommend using Firebase.
2. If the service will not be available then you can update your app after 10 years.
3. Maintaining a consistent network connection is likely to be less reliable than push notifications which is an OS feature.

You can try to implement your app as a location app. The OS will keep it running when it goes to the background.
 
Upvote 0
Top