DEV Community

Waldemar Panas
Waldemar Panas

Posted on • Originally published at on

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.x. Then, select RSocket as a dependency.

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


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

class ServerController {
    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.

class ClientConfiguration {
    internal fun rSocketRequester(rSocketStrategies: RSocketStrategies): RSocketRequester 
        = RSocketRequester.builder()

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

class ClientController(private val rSocketRequester: RSocketRequester) {
    fun hello(@PathVariable("name") name: String): Mono<String> = rSocketRequester.route("queue")
        .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 file.


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, i.e. 8081.


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

class ClientConfiguration {
    internal fun rSocketRequester(rSocketStrategies: RSocketStrategies): RSocketRequester {
        val uri = URI.create("ws://localhost:8080/rsocket")
        return RSocketRequester.builder()

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


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.

Top comments (0)