is there a kotlin native websocket client library ...
# getting-started
h
is there a kotlin native websocket client library or should we just proxy that to platform-specific code?
m
I have never tried but I thought you can use Ktor
h
it looked like that was jvm only but maybe i misunderstood it
h
what does "Ktor provides a WebSocket client for the following engines: CIO, OkHttp, Js" mean? I thought CIO and OkHttp were both JVM-only
m
I guess you are right. As I said, I have never worked with it 🤷
u
I haven't used the ktor ws client yet, but I have been using ktor http client on iOS as a part of multi platform project and it works fine so far.
h
ah, cool, i'll give it a go
@Ugi do you have an example of doing this? i've been struggling for the past few hours trying to get ktor to work but it seems like i don't have an event loop and i can't figure out how to create one. (I'm working on an iOS library, my app is swift, calling into the library that wants to use ktor.)
u
Hey @Hixie I'm currently at work, but I'll try to help you out in the evening, I also had some trouble with getting it to work but eventually sorted it out with coroutines
Hey @Hixie sorry for the delay, I had to look up the code to be sure how I implemented it. So, I'm using
1.3.5-native-mt
version of coroutines, but I think non-native-mt version should work as well. and then in the code that needs to do the actual http call, I use
Copy code
GlobalScope.launch(Dispatchers.Main) {
    ...
    httpClient.get<ResponseModel>("https://...")
   ...
}
That should get you going, at the moment I'm using a callback to return the results. I'm certain there are better ways of doing this (for starters not just using GlobalScope), but I haven't had the time to get to it yet.
h
when I tried that yesterday it told me that I didn't have an event loop.
but maybe i'm using the wrong "version of coroutines"? not sure how i select that
(i'm very new to kotlin and gradle)
u
Could you paste your build.gradle here?
h
let me throw it on github somewhere, hold on
(not sure that code even compiles right now, for the record; i just threw up whatever i had last looked at on thursday)
u
try adding implementation
org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.5-native-mt
to
Copy code
sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
        implementation("io.ktor:ktor-client-core:$ktor_version")
    }
and implementation
org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.5-native-mt
and
implementation("io.ktor:ktor-client-ios:1.3.5-native-mt)
to
Copy code
sourceSets["iosMain"].dependencies {
        implementation("io.ktor:ktor-client-ios:$ktor_version")
    }
I haven't tried compiling it, but maybe it will work :)
h
thanks, i will try!
"Could not find io.ktorktor client ios1.3.5-native-mt"
ah maybe that last implementation was just a typo
seems to find the dependencies without it
well i got it to build but now it just crashes in kotlin's library:
Copy code
Uncaught Kotlin exception: kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[WorkerCoroutineDispatcherImpl@25429a8, Continuation @ $connect$lambda-2COROUTINE$1]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
followed by a stack trace all in the kotlin libs followed by
Copy code
Caused by: kotlin.ClassCastException: kotlin.coroutines.native.internal.CompletedContinuation cannot be cast to kotlinx.coroutines.DispatchedContinuation
...followed by another stack also all in the kotlin libs
(i updated the code on github to reflect what i have) thanks for the help so far, by the way, much appreciated
aha, it was because my code was throwing an exception
thanks!
u
Oh that sucks 😞 But it seems it should be coming soon, judging by this https://github.com/ktorio/ktor/issues/1215
Hey @Hixie take a look at this as well, never used it before, but states that it has WS integration for multiplatform https://github.com/korlibs/korio
h
interesting stuff, will investigate. thanks!
based on https://korlibs.soywiz.com/korio/ it looks like it's android-only 😞
(and js, but not native/ios)