Home United States USA — software Getting Started With RSocket Kotlin

Getting Started With RSocket Kotlin

163
0
SHARE

Set up an RSocket client and server with some help from Kotlin and Ktor.
Join the DZone community and get the full member experience. RSocket is a transport protocol designed for reactive applications. More information on RSocket can be found on their website, leaving me to focus on writing about how RSocket and Kotlin can be combined. RSocket has several libraries written in various languages that implement the RSocket protocol. For Kotlin, this comes as an extension for Ktor (a Kotlin client and web-server library) named rsocket-kotlin. We will look at this extension through this post. The RSocket + Ktor libraries heavily use Kotlin’s coroutines and flows. I recommend looking at these first if you have not used them before. Writing the content for this post was the first experience I’ve had using flows, and although they resemble constructs I am used to, calling the right method at the right time still proved challenging. In this post, we will construct two services; one handling inbound requests via HTTP and a backend service that only exposes RSocket endpoints. This allows me to demonstrate both a working RSocket client and server, as well as fiddling around with some Ktor functionality to make it more interesting. In later sections, the inbound service will be known as the “client” and the backend service as the “server”. At the time of writing, you’ll want to use the following dependencies:
As you can see, the RSocket dependencies are all 0.x.x versions, and therefore the APIs fluctuate over time. Hopefully, they’ll remain slightly stable after I’ve written this so that I don’t have to update it… In the two sections below, representing the inbound and backend services, we will look at the foundational code that endpoints can be built onto. Here we have a HTTPClient, which will be used to make RSocket requests. I know it doesn’t look related to RSocket yet; right now, it’s not. It’s a plain old HTTPClient provided by Ktor; however, as you’ll see later, the RSocket client library provides extension functions that leverage a HTTPClient to make requests. The client installs WebSockets and RSocketSupport to add functionality via plugins for WebSockets and RSockets respectively. It is essential to point out here that WebSockets must be installed before RSocketSupport; otherwise, you will receive the following error:
Thankfully, the exception is nice and explicit and tells you how to rectify the issue. We then have the non-RSocket part, where a HTTP server is created and represents the entry point to the system. WebSockets are also installed here, which has no relation to RSocket itself but is used in this post’s examples to make them more interesting. Onto the backend service, which only provides RSocket endpoints, which the inbound service communicates with. This means there is no need for a HTTPClient to be created. This time around, the HTTP server provided by embeddedServer installs WebSockets and RSocketSupport. WebSockets should be installed before RSocketSupport here as well. In the following sections, we will look at examples for each RSocket request type, which include:
The examples in the following sections will detail both the client and server-side implementations of each RSocket request type. A Fire and forget request sends an endpoint some data and moves on, not expecting a response to be sent back. This allows for optimisations in both the client and the server implementations. From the RSocket documentation:
Fire-and-forget is an optimization of request/response that is useful when a response is not needed. It allows for significant performance optimizations, not just in saved network usage by skipping the response but also in client and server processing time as no bookkeeping is needed to wait for and associate a response or cancellation request. To send a fire-and-forget request, create a RSocket by calling rSocket on the HTTPClient made earlier.

Continue reading...