Hi team,
I'm working on an MQTT client for Apple platforms (macOS, iOS, and possibly tvOS and watchOS). I would like the client to listen to messages even when the application is in the background. I would appreciate any suggestions on the best approach to achieve this.
Based on iOS Background Execution Limits, it seems that my best bet is to use a long-running background process with BGProcessingTaskRequest while setting up the connection. Does that sound like the right approach? Is there any limits for the bg tasks?
I currently have a working BSD socket. I'm not sure if it is necessary to switch to the Network Framework to have the background task working, but I'm open to switching if it's necessary.
If the approach works, does that mean I could built a http client to process large upload/download tasks without using NSURLSession? As I'm working on a cross platform project, it would be benefit if I dont need a separate http client implementation for Apple.
Any insights on this topic would be greatly appreciated.
Additionally, it's off topic, but the link to "WWDC 2020 Session 10063 Background Execution Demystified" (https://developer.apple.com/videos/play/wwdc2020/10063/) is broken. Is there a way to access the content there?
Thanks in advance for your help and insights!
I'm working on an MQTT client for Apple platforms (macOS, iOS, and possibly tvOS and watchOS).
The story here varies by platform:
-
For macOS, it’s possible to run indefinitely in the background.
-
watchOS has significantly more restrictions than iOS. See TN3135 Low-level networking on watchOS for the details.
-
tvOS generally follows iOS, except that one of the common workarounds, push notifications, is not available there.
The rest of my response focuses on iOS (including iPadOS).
it seems that my best bet is to use a long-running background process with while setting up the connection. Does that sound like the right approach?
No. Quoting iOS Background Execution Limits:
To request extended background execution time, typically delivered overnight when the user is asleep, use
BGProcessingTaskRequest
.
So, unless you just happen to want to run these connections at some randomish time during the middle of the night (-: this is unlikely to be useful to you.
I currently have a working BSD socket. I'm not sure if it is necessary to switch to the Network Framework to have the background task working, but I'm open to switching if it's necessary.
Network framework doesn’t change this situation. BSD Sockets and Network framework are both low-level networking APIs, and they behave similarly when in comes to networking in the background.
As to what you should do, it very much depends on the specifics of your MQTT client. In many cases the best path forward is to offload this work to a server, and have it generate push notifications to notify the iPhone user of relevant conditions.
the link to "WWDC 2020 Session 10063 Background Execution Demystified" … is broken. Is there a way to access the content there?
That video is no longer available from Apple )-: I haven’t fixed the link because a) sometimes I’m able to ‘resurrect’ important videos like this one, and b) the details in the link make it easier to find related non-Apple resources.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"