Getting started with RSocket
Tags: rsocket, kotlin, spring, websocket
Published: 2020-01-20 17:38:30

---
title: Getting started with RSocket
published: true
date: 2019-07-22 00:00:00 UTC
tags: rsocket, kotlin, spring, websocket
canonical_url: https://wpanas.github.io/programming/2019/07/22/rsocket.html
---

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

# What is RSocket?

[RSocket](https://rsocket.io) 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](https://www.youtube.com/watch?v=dGNv-Djm7h0) 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](https://start.spring.io) and select Spring Boot, at least version 2.2.x. Then, select RSocket as a dependency.

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

```properties
spring.rsocket.server.transport=tcp
spring.rsocket.server.port=7000
```

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

```kotlin
@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](https://start.spring.io) 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.

```kotlin
@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.

```kotlin
@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.properties` file.

```properties
spring.rsocket.server.transport=websocket
spring.rsocket.server.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.properties`, i.e. `8081`.

```properties
server.port=8081
```

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

```kotlin
@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. For more information about RSocket in Spring Boot visit official [reference guide](https://docs.spring.io/spring-boot/docs/2.2.x/reference/htmlsingle/#boot-features-rsocket).