https://kotlinlang.org logo
#ktor
Title
# ktor
a

Ahmed na

09/07/2023, 9:54 AM
Hi we're using ktor websockes server and client for client it's multiplatform (android / iOS) our users are 50 % android 50% iOS but we noticed that android clients gets disconnected/re-connected more often than iOS (underlined session count indicate that android session are 3 time more than iOS) Also the session duration is short (5 seconds ) , our user app session is 2/3 minutes here is server config
Copy code
install(WebSockets) {
        pingPeriod = Duration.ofSeconds(20)
        timeout = Duration.ofSeconds(20)
        maxFrameSize = Long.MAX_VALUE
        masking = false
}
and here is client config
Copy code
install(WebSockets) {
   pingInterval = 30_000
}
I'm looking for configuration to stabilise the connection (longer session duration) without affect real time advantage and see why android is not as stable as iOS any recommendation ? is using a one minute ping interval safe ?
c

Cherrio LLC

09/07/2023, 5:43 PM
Do you know the cause of disconnection? Also I remembered we switched to CIO as engine don’t remember why though, but it helped to stabilize connection.
c

Christos Malliaridis

09/07/2023, 11:49 PM
As Ayodele mentioned its worth looking into the causes of the disconnections. I could imagine that Android lifecycles may disconnect your clients when the app is thrown into the background (e.g. when the user switches for a short time to another app to answer to a message). The behavior may vary between Android devices and versions, so analyzing this data may also be valuable.
a

Ahmed na

09/08/2023, 1:26 AM
@Cherrio LLC @Christos Malliaridis Server erros shows ping timeout this why i thought i might need to tweak it a bit , can i use CIO for server only ? or have to be both server and client
Copy code
java.io.IOException: Ping timeout

at io.ktor.websocket.DefaultWebSocketSessionImpl$runOrCancelPinger$newPinger$1.invokeSuspend ( io/ktor.websocket/DefaultWebSocketSession.kt:279 )
at io.ktor.websocket.DefaultWebSocketSessionImpl$runOrCancelPinger$newPinger$1.invoke
at io.ktor.websocket.DefaultWebSocketSessionImpl$runOrCancelPinger$newPinger$1.invoke
at io.ktor.websocket.PingPongKt$pinger$1.invokeSuspend ( io/ktor.websocket/PingPong.kt:86 )
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith ( kotlin/coroutines.jvm.internal/ContinuationImpl.kt:33 )
at kotlinx.coroutines.internal.ScopeCoroutine.afterResume ( kotlinx/coroutines.internal/Scopes.kt:33 )
at kotlinx.coroutines.AbstractCoroutine.resumeWith ( kotlinx/coroutines/AbstractCoroutine.kt:102 )
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith ( kotlin/coroutines.jvm.internal/ContinuationImpl.kt:46 )
at kotlinx.coroutines.DispatchedTask.run ( kotlinx/coroutines/DispatchedTask.kt:104 )
at io.netty.util.concurrent.AbstractEventExecutor.runTask ( io/netty.util.concurrent/AbstractEventExecutor.java:174 )
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute ( io/netty.util.concurrent/AbstractEventExecutor.java:167 )
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks ( io/netty.util.concurrent/SingleThreadEventExecutor.java:470 )
at io.netty.channel.nio.NioEventLoop.run ( io/netty.channel.nio/NioEventLoop.java:503 )
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run ( io/netty.util.concurrent/SingleThreadEventExecutor.java:997 )
at io.netty.util.internal.ThreadExecutorMap$2.run ( io/netty.util.internal/ThreadExecutorMap.java:74 )
at io.ktor.server.netty.EventLoopGroupProxy$Companion.create$lambda-1$lambda-0 ( io/ktor.server.netty/NettyApplicationEngine.kt:288 )
at io.netty.util.concurrent.FastThreadLocalRunnable.run ( io/netty.util.concurrent/FastThreadLocalRunnable.java:30 )
c

Christos Malliaridis

09/08/2023, 2:03 AM
If your client is not your server at the same time they are completely independent from one another and they could even be implemented in different programming languages, as long as they use the same protocol of communication (in that case websockets). To answer your question, you are completely free to change the engine on client and server side, as long as the selected engine supports websockets (see Ktor Engines - Limitations). A ping timeout usually occurs if the server does not receive a pong response from the client in time. This may be an indicator that the client is not implemented correctly or that the client disconnects under specific circumstances (e.g. app lifecycle changes).
👌 1
2 Views