LocalHost Websocket Server

Hi,


For some application on iOS we need to create a localhost socket server. Here a client socket application which also run on the same machine post messages to socket server. I have successfully acheived this architecture on android. I am getting issues on iOS side.


I already used the BlueSocket and SwiftSocket libraries to acheive the same but face some issues like this one:

https://github.com/IBM-Swift/BlueSocket/issues/179


Any kind of library,tutorial, demo, code suggestion is highly welcomed.


Thanks.

Replies

Here a client socket application which also run on the same machine post messages to socket server.

If I’m reading this correctly, your plan is to have two separate applications, one running a WebSocket server and the other connecting to it. Is that right?

If so, you’re going to run into problems here. The issue is not with the networking because with iOS’s execution model. As described in Technote 2277 Networking and Multitasking, an iOS app can use networking APIs in the background just fine, as long as its not suspended. Once it’s suspended, all bets are off. This is problematic in the case of your server app, where it’ll not be able to handle network requests after the use moves it to the background.

You can use a

UIApplication
background task to delay this for a while, but those only work for a very short period of time. Unless your server app is able to keep running in the background for some other reason, this architecture won’t work on iOS.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo,


Thanks for the reply. I am tryng to acheive the same as you mentioned. So isn't VOIP socket helps here?

https://developer.apple.com/library/archive/technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841-CH1-SUBSECTION15


My application aslo uses other resources like BLE, Push notification, WatchConnectivity etc.

So can't any of these help to keep application awake so that socket can work?

So isn't there any option available to do that in background?

So isn't VOIP socket helps here?

No. There are many problems with it:

  • The legacy VoIP architecture has been officially deprecated and is disabled if you link with a modern SDK. See QA1938 iOS 10 and the Legacy VoIP Architecture.

  • Even when it was available, it didn’t work very reliably (which was why it was deprecated).

  • It never supported listening sockets.

  • App Review requires that apps using the

    voip
    background mode provide user-level VoIP functionality. Specifically, clause 2.5.4 of the App Store Review Guidelines says:

Multitasking apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc.

So can't any of these help to keep application awake so that socket can work?

Not really. Most background execution facilities are built around the idea that the app is resumed (or relaunched) in the background and then shortly thereafter gets suspended again. The only way to run continuously in the background is if you’re doing things that are immediately obvious to the user (audio or continuous location tracking). This is not accidental. The goal here is to prevent apps burning through the user’s battery without them knowing about it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo,



Thanks for your valuable responses. They really helped us.

According to your previous reply audio or continuous location tracking can keep application awake in background.

This is true for audio as i tried it with audio that worked like a charm.

But when i tried to use continuous location tracking that didn't helped anyway. i have Privacy - Location Always Usage Description permission and Core Location set up in code where i receive location updates which is working fine.

But Socket remain closed in background in this case as well.

Is there any other permission i need to take or there is another setup also required to awake this.

I don’t have a good explanation for the location case. If you need a definitive answer for that, you should open a DTS tech support incident so that I and DTS’s location expert can fight over who gets to investigate this (-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"