Hey, I'm implementing SSE (Server-sent events) in ...
# ktor
z
Hey, I'm implementing SSE (Server-sent events) in Ktor client in an Android app. So far things seem to be working, but I'm having issues detecting connection drop. Imagine the case where the internet connection is lost while the SSE connection is alive. There's no way to detect that this has happened - no exceptions are thrown. And if the internet is reconnected, no more messages are received in:
Copy code
httpClient.sseSession("bla-bla.com/sse").incoming.collect { ... }
Is there any way to detect such scenarios and probably I'd have to disconnect and re-connect the
sseSession
? I came across this thread but there's no activity on the ticket. The only idea I have is to use the Android's APIs for detecting internet connectivity and disconnect/connect the SSE session based on it (although the Android API not 100% reliable). I also don't know yet what will happen if the server drops the connection - will the client disconnect at some point or not.
a
Can you please describe a specific use case where the absence of the connection should be detected? In my opinion, the only reliable way is to send and receive ping/pong messages and detect a timeout when the pong wasn't received.
z
My problem is that when the internet connection drops and is then re-established, the SSE event receiving stops. And being a mobile app, the internet may drop at any point. That's why I was looking into ways to detect absence of connection. But if Ktor can continue receiving SSE events even after a temporary lack of connectivity, then I don't need the detection of absent connection.
a
I think an automatic reconnection should be implemented as part of the KTOR-6242.
z
Ahaaa. So KTOR-6242 will handle this. I wasn't sure if it covers my case or not. Thanks for clarifying that! gratitude thank you
144 Views