Most APIs that push data hand you a callback. Kotlin code would rather have a Flow . callbackFlow is the bridge, and
it is short, but two details decide whether your version leaks sockets: where the cleanup goes, and whether the Flow is
cold.
To have something real to wrap, this article uses a public blockchain network that emits a block every 15 seconds over
a WebSocket. It needs no API key and no account, so the code runs as written. You do not need to know anything about
the chain; treat it as a server that pushes one event every 15 seconds.
The project
build.gradle.kts :
plugins { kotlin ( "jvm" ) version "2.2.0" application }
repositories { mavenCentral ( ) }
dependencies { implementation ( "org.janzeer:janzeer-sdk-client:0.1.7" ) implementation ( "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2" ) }
kotlin { jvmToolchain ( 17 ) } application { mainClass .…