Search code examples
javascriptandroidmobileserver-sent-events

EventSource disconnections on sleeping mobile devices


When my phone is being used, a disconnection between the browser and server prompts a retry every 3 seconds.

However when the phone is put to sleep for a minute or so, the EventSource's readyState becomes 2, CLOSED. I can't find any way to recover from this condition without re-making the EventSource. onerror does not fire.

Am I using the EventSource API incorrectly, or is this normal behavior? Needing to write a heartbeat to detect readyState === 2 would seem to negate one of SSE's purported features, automatic reconnection.

Thanks for any help.


Solution

  • The old SSE spec said that SSE should give up on DNS error. If your phone loses Internet connection when sleeping, then upon reconnection the DNS query would fail and close SSE permanently :(