Hello, I’m starting to add Ktor
versus Retrofit/Spring
in my projects and there is an overwhelming abundance of engines available: Engines | Ktor
What is the principal difference between them, which are the most promising and which are the legacy?
Should I use Curl
engine if my app runs on Linux x64
or CIO
is the universal fits-for-all engine, so there is no need to scatter the resources if I need to implement Ktor
both on the server side and in an Android
app?
First of all, you are mixing server engines and client engines. Curl is client, while the link you presented leads to servers. As for server engines, the primary ones are netty
- and it is your default choice since it is a well established technology and cio
which is developed fully by ktor team. The last one is not yet as stable, but probably have more perspectives in future because it is kotlin-first (could be implemented in multiplatform) and uses coroutines in its core.
Thanks. Also I don’t understand CIO: I can’t run the request within coroutineScope
but can within runBlocking
context. Maybe I don’t understood something else, but what does the “coroutine-based engine” mean?
The API is exactly the same for netty
and cio
. The difference is inside. And in order to run something suspended, you need to be in suspended world. So either use runBlocking
or launch
.
According to this issue https://youtrack.jetbrains.com/issue/KTOR-889
“Currently the only option that seems to work with GraalVM is CIO (Without logging)”.
So, if you want/need to run on GraalVM you must use CIO.
Hello, what is the current situation with the engines? As I see, the documentation hasn’t changed much and CIO still “supports only HTTP/1.x for now.” I have issues using io.ktor:ktor-client-logging
with it because it demands Android 26+ SDK and I need to use 21. As I understood, CIO is the only engine supports multiple async requests with coroutines, right? But most of the time it comes with runBlocking
for me, so what should I use for Android and Kotlin Multiplatform Mobile here? And how can I add logging for Ktor easily?
Found an answer for logging: possible to use custom-maden methods like this:
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
Timber.i(message)
}
}
level = LogLevel.ALL
}