Getting started with RSocket

In this article I will present you how to use RSocket with Spring Boot.

What is RSocket?

RSocket is a bi-directional message-driven communication protocol. It features an advanced communication flow beyond standard request/response. You can response with a single message, a stream of messages or don’t response at all. A server can also start a communication on an established channel with a client.

RSocket allows you to communicate using following transport protocols:

  • TCP
  • WebSocket
  • Argon
  • HTTP/2 Stream

If you want to know more, you can watch Ben Hale’s presentation from Spring I/0 2019.

RSocket TCP Server

RSocket server is the easiest part of the whole project. All you need to do is to go to Spring Initializr and select Spring Boot, at least version 2.2.0.M4. Then, select RSocket as a dependency.

To configure RSocket server with TCP protocol, add following lines to application.yaml. This configuration will open RSocket at port 7000.

spring:
    rsocket:
        server:
            transport: tcp
            port: 7000

Last step required is to add some controllers and you are ready with the server. You can try something like that:

@Controller
class ServerController {
    @MessageMapping("queue")
    fun helloQueue(incomingMessage: Message): Mono<Message> 
        = Mono.just(Message("Hello ${incomingMessage.message}"))
}

data class Message(val message: String)

RSocket TCP Client

Client application is very similar. Go to Spring Initializr and bootstrap it with RSocket and Spring Reactive Web.

To communicate with RSocket over a TCP port 7000, you need to create a RSocketRequester. For the TCP protocol it can look like this.

@Configuration
class ClientConfiguration {
    @Bean
    internal fun rSocketRequester(rSocketStrategies: RSocketStrategies): RSocketRequester 
        = RSocketRequester.builder()
            .rsocketStrategies(rSocketStrategies)
            .connect(TcpClientTransport.create(7000))
            .block()!!
}

Your client application can be a standard reactive web application that uses HTTP. To test communication with configured server add a following endpoint.

@RestController
class ClientController(private val rSocketRequester: RSocketRequester) {
    @GetMapping("/hello/{name}")
    fun hello(@PathVariable("name") name: String): Mono<String> = rSocketRequester.route("queue")
        .data(Message(name))
        .retrieveMono(Message::class.java)
        .map { it.message }
}

data class Message(val message: String)

RSocket WebSocket server

WebSocket requires you to add Spring Reactive Web dependency to server application. RSocket port will be the same as Web application port. Instead configuring port, you need to define mapping for RSocket endpoint. In this example, you will use /rsocket path. Configure following properties in application.yaml file.

spring:
    rsocket:
        server:
            transport: websocket
            mapping-path: /rsocket

Controllers that you created for TCP server don’t need to change.

RSocket WebSocket client

Your server might be already started at port 8080. Change client port to another in application.yaml, i.e. 8081.

server.port: 8081

Configuration for client with WebSocket transport protocol is like previous one. All you need to do is provide ClientTransport.

@Configuration
class ClientConfiguration {
    @Bean
    internal fun rSocketRequester(rSocketStrategies: RSocketStrategies): RSocketRequester {
        val uri = URI.create("ws://localhost:8080/rsocket")
        return RSocketRequester.builder()
            .rsocketStrategies(rSocketStrategies)
            .connect(WebsocketClientTransport.create(uri))
            .block()!!
    }
}

Controllers that you created for TCP client don’t need to change.

Summary

Spring Boot made it simple to create application that use RSocket. I hope that those examples show you how to do it. Version 2.2.0.M4 is not production ready yet, but you can try it out in your reactive applications. For more information about RSocket in Spring Boot visit official reference guide.

comments powered by Disqus