I have an one basic doubt that hasn't been found yet after looking in search and stackoverflow. I have a long polling backend implementation in PHP. The logic there is that connection remains alive until new message is found in server. It's an infinite loop where inside either it returns the new message if available and breaks the connection or it doesn't send any response if no new messages are available. Everything is fine. There is no problem with backend and concept of long polling.
My main problem or limitation I am facing is in frontend which is iOS. I have NSURLRequest to setup connection to my "long polling" backend but my front end request connection has default time out period that breaks the connection when it reaches that timeout interval. This is my limitation here. According to apple documentation they have an initializer that takes an argument of custom timeout interval but again my question remains what value of interval should I provide here? https://developer.apple.com/documentation/foundation/nsurlrequest/1528579-requestwithurl
In scenario where it's unpredictable when new messages will be available I don't know what is the recommended value of timeout interval to provide. My question is to know if there is value that indicates infinite timeout interval? If not then what do you recommend to use timeout value and when that timeout value expires and closes connection then How do I handle another polling session smoothly in front end.
My objective is to avoid making more http connection which is already prevented by using long polling way. But if my front end connection is timed out lets say every 60 seconds then I have to initiate another request. If that is the only way then I am fine but If there is a way to keep my connection alive in frontend until backend closes it then please let me know.
Answers to solution could be both swift or objC here. I just need conceptual answer.
EDIT SOLUTION: I am not sure If I should mark it solved by posting in answers. But My solutions seems to be the absolute value that represents near infinity for NSTimeInterval which is double under the hood. Here is the link: What constant represents "never" for an NSTimeInterval?
Set the timeout of the request to .infinity
, this will keeps connection alive for ever (if there is no issues with the network or etc.)
Since there may be a network issue, or backgrounding issue or etc., iOS may close the connection regardless of the timeout. So you must listen for the failure situations in the response callback (or delegate), and retry the request if needed.