How to keep socket alive in ios? - ios

I'm doing a VOIP project on iPhone. I have the problem to keep the socket alive.
I already configured one socket for VOIP usage, and set kCFStreamNetworkServiceTypeVoIP for CFReadStreamRef and CFWriteStreamRef.
When my app runs in the foreground and I don't do any action, after 1 minute iPhone will sleep, black screen. My socket is still connected to the server.
But when my app runs in the foreground I press home button, and don't do any action, about 1 minute iPhone will sleep, then my socket disconnects from server.
Please help me.

First, have you read Tips for Developing a VoIP App? From your description, it sounds like you haven't set UIBackgroundModes to voip. You also likely will need to configure a keep alive handler.

Related

Codename One - Socket closed when device is in standby

I'm developing an iOS app, using Codename One. I extended the SocketConnection class, in order to receive data from a server.
I tested the app on an iPhone 7s (iOS version 11.2.5 15D60) and it works fine. When I press the home button, the app suspends, but it keeps the connection with the socket server. But when the device goes into standby mode, the socket connection is closed.
Is it possible to avoid this behaviour, in order to keep the socket connected?
Short answer: No and that's a good thing.
Long answer: When an app is suspended in iOS if it uses too much resources the OS kills the app to conserve battery life. Apple does allow some apps in some cases to perform tasks in the background but even then it's under heavy restrictions that are hard to follow and change frequently.
Generally you would define a background behavior that allows you to do something in the background. E.g. in this post we discuss background fetch which allows you to keep polling a server when we are in the background. It might not work with sockets.
The "right thing" in terms of iOS would be suspending your connection when in the background and using push notification to notify the user of a potential update.

NSURLSessionconfiguration.shouldUseExtendedBackgroundIdleMode : what does it do?

Could not find any documentation on this instance variable.
Is it related at all to being able to handle the response to a network request even when the app has been moved to the background?
From the SDK's NSURLSession class comments:
"Enable extended background idle mode for any tcp sockets created. Enabling this mode asks the system to keep the socket open and delay reclaiming it when the process moves to the background."
So, it looks like this is aimed at prolonging a network connection that is active when the user sends the app from the foreground to the background and it enters suspended state.

Keeping iOS socket alive in background mode without VOIP

We have an game that creates a player lobby using sockets. When the player creates a lobby, a game is created on our server. Unfortunately if the user switches our app away with the Apple button, another user may join the game and the game will start with just one player active. So we want to send a message to our server via a socket when the app transitions to the background to close the lobby, stopping others joining.
But we can't. The sockets get frozen on entering background mode. Even if we keep the app alive a while using beginBackgroundTaskWithExpirationHandler, no socket activity works.
VOIP is not an option, as the title is not VOIP related and will get bounced immediately from Apple submission.
Any one got any non-VOIP related ideas?
Many thanks,
Steve.
I think what you want to do is to let the server know that this user is off-line when touching the Apple Home button.
I am using GCDAsyncSocket, but other ways may be similar.
What I'm doing is
Add observer on UIApplicationDidEnterBackgroundNotification.
When application enter background then call socket disconnect. The server will know that you are off-line.
I know this is a little late for your answer, but could be helpful for others.

How to prevent Web Socket from disconnecting while iPhone/iPad goes in lock state?

We have implemented HTML5 Web socket in my iPhone and iPad app. Web Socket works fine when app goes background (iPad/iPhone is not in lock state). But it gets disconnect when iPhone or iPad gets lock.
Is there any way to keep web socket running even when iPhone/iPad is lock?
Thanks.
According to my own experiments. When you lock the device, the device seems to shut down WiFi and close network sockets, most likely in order to save power. The mobile broadband connections remains active though, and sockets seem to survive. At least for some time.
Have you tried to wrap the some parts of the socket code inside a beginBackgroundTaskWithExpirationHandler()? This should at least allow you to gracefully close the socket, or keep the socket alive for some time. The OS will, however, kill your task it after a global timer expires.

iOS VOIP app does not accept new socket connections in background

I am trying to implement an iOS VOIP application.
I am using GCDAsyncSocket to listen on a port for connections. I did add the required background modes in info.plist and also registered the readstream and writestream to kcfNetworkServiceTypeVOIP.
I also added KeepAliveTimeOutHandler to wake up the app. The handler block just prints the wake up time to stdout.
As long as app is in foreground new connections are accepted but when the app is in background, the app is not waken and all the connections attempted are sent to the socket when the app moves to foreground again.
I searched through many solutions but could not get it to work.
Thanks in advance.
I am not sure if it is still relevant for you but in case someone else reads this question.
As discussed in other similar question on stackoverflow in this one for example you can have only one socket which is in NetworkServiceTypeVOIP mode. Otherwise both of them will not work and your app will not wake up and not accept new connections.

Resources