liveBook · Manning

RSocket server

As explained in Spring in Action, Sixth Edition

An RSocket server is a TCP-based endpoint that accepts and handles RSocket requests on a configured port.

An RSocket server is a TCP endpoint that accepts requests on configured routes and returns reactive responses. In Spring, create one with a controller, much like a web or REST controller, but map messages with @MessageMapping instead of HTTP paths. section 14.2.1

Listing 14.2 A simple RSocket request-response server
package rsocket;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
 
@Controller
@Slf4j
public class GreetingController {
 
    @MessageMapping("greeting")
    public Mono<String> handleGreeting(Mono<String> greetingMono) {
        return greetingMono
            .doOnNext(greeting -> 
                log.info("Received a greeting: {}", greeting))
            .map(greeting -> "Hello back to you!");
    }
    
}

A minimal controller receives a Mono<String>, logs the greeting, and maps it to a response. section 14.2.1

Build the endpoint by combining three pieces:

  • Annotate a controller method with @MessageMapping("greeting") to handle messages sent to the greeting route. section 14.2.1
  • Accept the incoming payload as Mono<String> and use map() to create the response Mono. section 14.2.1
  • Set spring.rsocket.server.port, such as 7000, so clients know where to connect. section 14.2.1
  • Add route variables such as greeting/{name}, then read them with @DestinationVariable. section 14.2.1

Client connection

An RSocketRequester connects over TCP with requesterBuilder.tcp("localhost", 7000), sends data through route("greeting"), and retrieves a typed Mono response. section 14.2.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains controller mapping, reactive payloads, route variables, client connection, and port configuration through working examples.
Examples
Uses greeting routes, personalized name placeholders, a GreetingController, port 7000, and RSocketRequester client calls.
What you can do after
Build it from scratch by creating a Spring controller, configuring its TCP port, and handling request-response messages.
How it connects
Connects RSocket servers with Spring controllers, Spring MVC conventions, Reactor Mono, and RSocketRequester clients.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.