https://kotlinlang.org logo
Title
h

Hixie

05/19/2020, 3:32 AM
is there a kotlin native websocket client library or should we just proxy that to platform-specific code?
m

Milan Hruban

05/19/2020, 4:49 AM
I have never tried but I thought you can use Ktor
h

Hixie

05/19/2020, 5:37 AM
it looked like that was jvm only but maybe i misunderstood it
h

Hixie

05/19/2020, 5:42 AM
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

Milan Hruban

05/19/2020, 6:16 AM
I guess you are right. As I said, I have never worked with it 🤷
u

Ugi

05/19/2020, 6:01 PM
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

Hixie

05/19/2020, 8:17 PM
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

Ugi

05/22/2020, 9:52 AM
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
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

Hixie

05/23/2020, 7:19 PM
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

Ugi

05/23/2020, 8:34 PM
Could you paste your build.gradle here?
h

Hixie

05/24/2020, 8:23 AM
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

Ugi

05/24/2020, 4:42 PM
try adding implementation
org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.5-native-mt
to
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
sourceSets["iosMain"].dependencies {
        implementation("io.ktor:ktor-client-ios:$ktor_version")
    }
I haven't tried compiling it, but maybe it will work :)
h

Hixie

05/24/2020, 8:18 PM
thanks, i will try!
"Could not find io.ktor:ktor-client-ios:1.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:
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
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!
Looks like the ktor library just doesn't support WebSocket on Kotlin/Native on iOS yet. (https://github.com/ktorio/ktor/issues/1894) Anyone else have any suggestions for a WebSocket library for Kotlin/Native on iOS?
u

Ugi

05/26/2020, 7:30 AM
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

Hixie

05/26/2020, 7:52 PM
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)