Hi, I'm using ktor in a Kotlin multiplatform proje...
# ktor
r
Hi, I'm using ktor in a Kotlin multiplatform project that supports jvm, native, and node-js. I'm trying to get the socket client example working, but: 1.
<http://Dispatchers.IO|Dispatchers.IO>
from kotlinx.coroutines is jvm only. 2.
runBlocking
from kotlinx.coroutines is jvm and native only. Is there a multiplatform example, or platform-specific examples for native and node-js?
b
Node is single threaded so switching dispatchers does almost nothing - I'd just expect typealias into main dispatcher on node. As for runBlocking, again that's impossible on js due to singleThreaded nature (although node does have some blocking io)
Generally runBlocking usage is discouraged if you can avoid it.
Hope this helps
r
Trying to use the ktor APIs without runBlocking results in a
Suspend function 'readUTF8Line' should be called only from a coroutine or another suspend function
error. I don't particularly care about it being multi-threaded, or runBlocking specifically, just how to implement a functioning ktor socket client in multiplatform. -- I'm mentioning runBlocking here as that is what the ktor sample code is using. (Note: I'm new to Kotlin coroutines and ktor sockets, so I don't know the best practices.)
b
One way to solve this would be to make your entire function chain suspend (all the way up to main)
Alternatively coroutines have some helpers to integrate with promises
r
Thanks. I'll investigate those options.
After further investigation, I've found that the sockets library only works for JVM. Given that, and the lack of other channels JSON-RPC/LSP uses (stdio, windows pipes, node-ipc, etc.) I've decided to not use ktor to provide the IO support.