Is it possible to perform a ping pong kinda operat...
# ktor
t
Is it possible to perform a ping pong kinda operation for ktor websockets client in order to keep it alive for a long duration?? I thought of having my server send a message every few second just to keep it alive until i close it myself. Any opinion??
o
Ktor already does this: https://ktor.io/docs/websocket-client.html#configure_plugin What are you missing?
t
when I initially read this doc, I thought it was works only for websocket servers, after implementing which didnt receive any messages and digging through the web, I found this : https://github.com/ktorio/ktor/issues/1418 https://youtrack.jetbrains.com/issue/KTOR-535 But on using
RawWebsocketSession
as mentioned there I had to use
sendSerializedBase()
, and that throws this error
This API is internal in Ktor and should not be used. It could be removed or changed without notice.
o
I'm still wondering what you're missing. Ktor keeps the connection alive via ping/pong without bothering you. If you go with the defaults, you won't even notice ping/pong messages (though they are there, working for you). Why would you want to use
RawWebsocketSession
? What's your use case?
t
My usecase involves keeping the socket alive for long atleast until you close the app I get cancellation exception after about 1 minute of no action with the websockets. Which made me wondered how to keep it alive for longer and I found the ping/pong way of doing it and I now need to make sure it's working on ktor websocket client(seeing the logs or something just to indicate that it's truly there) I get that the ping/pong is done without bothering me, but why cant I observe it with
incoming.receiveAsFlow()
o
Do you have a reproducer? The Ktor chat sample does keep the connection open indefinitely without any extra effort. The reason that you can't observe the ping/pong is probably as usual: Ping/pong sits on a lower layer of your protocol stack. So it is an implementation detail that is not intended to be noticed by upper layers (like you wouldn't expect to see ACK messages when sending data over TCP). Using the Ktor chat sample, you can check ping/pong on an idle connection if you add
<logger name="io.ktor" level="trace"/>
to
chat/src/backendMain/resources/logback.xml
and start the sample with an environment variable `KTOR_LOG_LEVEL=TRACE ./gradlew run`:
Copy code
2024-01-07 15:42:44.997 [eventLoopGroupProxy-3-1] TRACE io.ktor.websocket.WebSocket - WebSocket Pinger: sending ping frame
2024-01-07 15:42:44.999 [eventLoopGroupProxy-3-2] TRACE io.ktor.websocket.WebSocket - WebSocketSession(StandaloneCoroutine{Active}@431251b0) receiving frame Frame PONG (fin=true, buffer len = 76)
2024-01-07 15:42:45.000 [eventLoopGroupProxy-3-3] TRACE io.ktor.websocket.WebSocket - WebSocket Pinger: received valid pong frame Frame PONG (fin=true, buffer len = 76)
t
Oh... I see, I think I get you now But doesn't this mean that even if it's a lower layer of protocol(I dont know much about backend and networks), it's only observable on the server side(backend)?
o
You can also observe it on the client, which responds with "pong" messages. I have not tried to enable logging on the client, but it's there in the
ponger
function: https://github.com/ktorio/ktor/blob/main/ktor-shared/ktor-websockets/common/src/io/ktor/websocket/PingPong.kt
t
I'll try it and check it out again Thanks for this
👍 1
161 Views