Sending Java/Kotlin objects to RSocket route

336 views Asked by At

I've set up @MessageMapping endpoint using Spring's RSocket support and tested it with RSocketRequester. This flow is working perfectly since Spring handles bunch of low-level stuff for us.

Now, I'd like to have some sort of UI to show whatever data I'm working with and do any UI updates on that data through mentioned @MessageMapping endpoint. The problem is that I have no idea how to send full Java/Kotlin object (as JSON) to my endpoint using either rsocket-java or rsocket-kotlin libraries. Since I have to send both data and metadata and metadata is encoded as ByteBuf I had to encode JSON to ByteBuf, but it comes to endpoint all messed up and it cannot be deserialized.

I've also tried sending raw string value and it also comes to endpoint all messed up (bunch of non-UTF chars). I've tried both APPLICATION_JSON and TEXT_PLAIN for data mime type, but it didn't work for either.

Also, I guess whatever I'm doing wrong is not strictly related to Java/Kotlin libraries, but can be applied to any other existing RSocket library (Go, Rust, JS, etc.)

2

There are 2 answers

0
Sven Vidak On

I guess I've figured it out.

I've scratched that binary encoded metadata and borrowed what I found on the internet for RSocket in JavaScript and it worked :)

So, the solution is to use routing metadata mime type (I was using composite since example I found was using it) and the payload then can be created like this:

val json = "{}"
val route = "myawesomeroute"
DefaultPayload.create(<json>, "${route.length.toChar()}$route")
0
Yuri Schimke On

If you are using an rsocket-kotlin client (not building on spring-boot) then you can copy the code from https://github.com/rsocket/rsocket-cli/blob/master/src/main/kotlin/io/rsocket/cli/Main.kt

It builds metadata for the route with

    this.route != null -> {
      CompositeMetadata(RoutingMetadata(route!!)).toPacket().readBytes()
    }