How to keep a socket server in an iOS app alive when app goes to background or suspended?

Hi all,

My application requires to create a WebSocket server on an iOS application for other devices can connect and transfer data with my application. I used Vapor library to create a socket server and it works well when the application is in the foreground.

I am trying to keep the server alive when my app moves to the background or the suspended state so that my app and other devices can continue to communicate with each other.

Is there any ways to achieve that?

I tried to turn on a mode: "Audio, Airplay, and Picture in Picture" in background modes section in Signing & Capabilities and then my application can still communicate with clients when it is background mode.

But my application is an application for user can edit image and send it to other devices through sockets and it does not have audio, airplay,.. feature.

Is it ok to publish the app to the app store in the future?

Thank you!

Keeping a WebSocket server alive in the background of an iOS app can be a bit tricky, but it's definitely possible to achieve. While enabling background modes like "Audio, Airplay, and Picture in Picture" might keep your app alive in the background, it's important to use these modes for their intended purposes only to comply with Apple's guidelines for App Store submission.

Since your app doesn't include audio or airplay features, enabling those background modes might not be the best approach. Instead, consider using the "Background Processing" background mode, which allows your app to perform certain tasks, such as network communication, in the background.

Here's a brief outline of how you can accomplish this:

  • Enable the "Background Processing" background mode in your Xcode project settings.
  • Utilize URLSession with background configurations to handle WebSocket communication tasks in the background.
  • Make sure to adhere to Apple's guidelines and best practices for handling background execution, including proper management of background tasks and network connections.
  • By following these steps, you should be able to keep your WebSocket server running in the background of your iOS app without violating Apple's guidelines. This approach will ensure that your app can continue to communicate with other devices even when it's not in the foreground.

Hope this helps! Let me know if you have any further questions.

I recommend that you start by reading iOS Background Execution Limits.

Is it ok to publish the app to the app store in the future?

I don’t work for App Review and can’t make definitive statements on their behalf. The link above quotes the relevant guideline.

My application requires to create a WebSocket server on an iOS application for other devices can connect and transfer data with my application.

Can you explain more about this setup? Are these other iOS devices? And why do they need to work with your app directly?

Most folks in this situation have all the apps cooperate via some server infrastructure.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to keep a socket server in an iOS app alive when app goes to background or suspended?
 
 
Q