Lumenforge
Your profile — photo by Jake Nackos on Unsplash

Session host drops the client after 8 seconds while the app is backgrounded

norvane_dev — photo by Korede Adenola on Pexels
norvane_dev Mar 17

Hey. I have a co-op title for mobile built on Statecast and the hosted Session service. When the player sends the app to the background (home screen, or switching to another app) and then returns to the match, I get this in the device log:

-> sessionWillEnterForeground()-> sessionDidBecomeActive()Received error message from Session: peer timed out due to inactivity.Session lease is invalid. See TransportDriver.GetSessionLeaseStatus and SessionConnecWebSocketInvalidStateException: WebSocket is not in open state. 

I know about the 8 second lease rule1 in the Session docs. But my actual question is: what is the best way to hold the connection open while the app is parked in the background?

Solved by quartzloop in post #6

As far as I understand it, that is the case — and it is sort of expected. The player is sitting in the middle of a realtime match. You would not want them idle for more than a few seconds in a ranked lobby either. On desktop you are free to allow …

1,247views
3links
Participant — photo by Thanh Xuan Nguyen Ly on Pexels
Participant — photo by Prince on Pexels
Participant — photo by Foto Sushi on Pexels
quartzloop — photo by Ben Khatry on Pexels
quartzloop Guidance Steward Mar 17

Backgrounded apps on iOS lose their run loop within a second or two, so nothing on your side can keep the socket warm — the OS is going to suspend the thread regardless of what the transport wants.

The pattern that works: treat the background trip as a disconnect you planned for. Snapshot the local session lease on sessionWillResignActive(), tear the socket down cleanly, and reclaim the same lease on resume. The host keeps the slot reserved for 45 seconds, which is enough for almost every real-world app switch.