Announcing Krosstalk, a fully multiplatform pure K...
# feed
r
Announcing Krosstalk, a fully multiplatform pure Kotlin RPC library. Serialization, client, and server implementations are pluggable (Ktor and kotlinx.serialization are provided), and using `expect`/`actual` to enforce method signatures and share configuration is fully supported (and is in fact the main intended use case). Once you configure common, client, and server using an
expect object
(
MyKrosstalk
), declaring a RPC method is as simple as:
Copy code
// common
@KrosstalkMethod(MyKrosstalk::class)
expect suspend fun basicTest(n: Int): List<String>

// client (i.e. js)
actual suspend fun basicTest(n: Int): List<String> = krosstalkCall()

// server (i.e. jvm)
actual suspend fun basicTest(n: Int): List<String> = List(n) { "$it" }
See the README for the full example, and details. https://github.com/rnett/krosstalk
🌟 3
p
looks nice! I was trying to achieve something similar with https://github.com/krzema12/kotlin-rpc/ but then learned that KVision has something like this (CC: @Robert Jaros). I'm wondering if we should try to make it 1 instead of 3, the goals seem very close 🙂
b
What does that annotation do?
n
seems to be used by a compiler plugin to generate or modify code .. i think that should be in the readme and demo..
Copy code
plugins {
    id("com.github.rnett.krosstalk")
}
c
can it generate restful routes? (post for create, put for update, etc)
👍 1
b
I'm not too interested in rpc, but compiler plugins are definitely on my radar. So good find! Thanks for sharing. It'd be great if you could document your journey writing it in dev.to 🙏
r
@christophsturm Yes, if you use endpoints. It generates them internally anyways, but they will be named things like
POST krosstalk/myMethod_12f45
, so if you actually want to call them externally use
@KrosstalkEndpoint
.
@Nikky Note the
Gradle plugin (required)
in the readme. I should probably make it a bit more obvious.
@Big Chungus the compiler plugin is actually fairly simple, as far as compiler plugins go, it just registers the method by adding an init block to the object in
@KrosstalkMethod
, and on the client side, replaces
krosstalkCall()
with something like
MyKrosstalk.call(mapOf("n" to n))
(for the example)
b
That's exactly what I need for my project, but there are very little resources to get started with compiler plugins, unfortunately.
r
I'd recommend [this blog](https://bnorm.medium.com/writing-your-second-kotlin-compiler-plugin-part-1-project-setup-7b05c7d93f6c), and [this one for some tips](https://dev.to/shikasd/kotlin-compiler-plugins-and-binaries-on-multiplatform-139e). Most of the work is going to be familiarizing yourself with the IR classes, finding edge cases, and reporting bugs on youtrack. There was a whole lot of "try it and see", but my use at least was pretty straightforward.
👍 1
@Piotr Krzemiński It looks like you were using codegen by a gradle task, and kvision is using a annotation processor (It's used via compiler plugin but generates text, I have no idea how that works). KVisions is a lot more opinionated, which I think is probably intentional, so I don't know how much we would mesh. Other than that though, I'm happy to have issues or PRs. Especially for plugins.
Ktor is also working on something similar with their Locations, I think.