Backend Development 7 min read

Mastering RSocket with Spring Boot: Build Request‑Response, Fire‑Forget, Stream & Channel

This tutorial introduces RSocket—a reactive, binary, multiplexed protocol—and demonstrates how to install the RSC client, create a Spring Boot RSocket server, and implement all four interaction models (request‑response, fire‑forget, stream, and channel) with complete code examples.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Mastering RSocket with Spring Boot: Build Request‑Response, Fire‑Forget, Stream & Channel

About RSocket

RSocket is a new, language‑agnostic, layer‑7 application network protocol. It is a bidirectional, multiplexed, message‑based binary protocol with reactive‑stream back‑pressure.

Unlike the traditional HTTP request/response model, RSocket also supports Fire‑And‑Forget (no response), Stream (one‑way stream), and Channel (bidirectional stream).

Client implementations exist for many common languages, greatly expanding network interaction scenarios such as instant messaging, service communication, and IoT.

Dubbo 3.0 has embraced RSocket, providing reactive programming support.

With the release of Spring Boot 2.4, Spring’s RSocket support has matured; this article shows how to use Spring Boot to implement the four RSocket interaction models.

Install RSC Client

The rsc client makes debugging RSocket calls as easy as using an HTTP Postman tool.

Download the latest version from https://github.com/making/rsc/releases.

Check the installed version with the command below.

<code>./rsc --version
{"version":"0.7.0","build":"2021-01-08T01:52:51Z","rsocket-java":"1.1.0"}
</code>

Create RSocket Server

Create a Spring Boot project and add the following dependency.

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-rsocket&lt;/artifactId&gt;
&lt;/dependency&gt;
</code>

Specify the RSocket server port.

<code>spring.rsocket.server.port=8848
</code>

Request‑Response (RR) Model

Define a route with @MessageMapping.

<code>@Controller
public class GreetingsController {
  @MessageMapping("request-response")
  Mono<String> reqResponse(@Payload String payload) {
    log.info("Received RR request: {}", payload);
    return Mono.just("Hello, " + payload);
  }
}
</code>

Invoke the server using rsc and receive the response.

<code>./rsc tcp://localhost:8848 -r request-response -d 'lengleng'
Hello, lengleng!
</code>

Add the --debug flag to view the full request/response frames.

<code>./rsc tcp://localhost:8848 -r request-response -d 'lengleng' --debug
... (debug frame logs) ...
</code>

Fire‑And‑Forget (FNF) Model

FNF does not return a result (Mono ).

<code>@MessageMapping("fire-forget")
Mono<Void> fnf(@Payload String payload) {
  log.info("Received FNF request: {}", payload);
  return Mono.empty();
}
</code>

Call the endpoint with rsc; no response is shown.

<code>./rsc tcp://localhost:8848 -r fire-forget -d --fnf
</code>

Single Stream Model

Use Flux to emit a stream of messages, e.g., one per second.

<code>@MessageMapping("stream")
Flux<String> stream(@Payload String payload) {
  return Flux.interval(Duration.ofSeconds(1))
             .map(i -> payload + LocalDateTime.now());
}
</code>

Request the stream with rsc.

<code>./rsc tcp://localhost:8848 -r stream -d 'lengleng' --stream
lengleng2021-01-19T21:34:10.700473
lengleng2021-01-19T21:34:11.696332
...
</code>

Bidirectional Channel Model

Both input and output are Flux streams.

<code>@MessageMapping("channel")
Flux<String> channel(Flux<String> settings) {
  return settings.map(s -> "你好 " + s + LocalDateTime.now());
}
</code>

Open a channel with rsc and send messages.

<code>./rsc tcp://localhost:8848 -r channel -d - --channel
lengleng
你好 lengleng2021-01-19T21:42:48.010415
spring
你好 spring2021-01-19T21:42:51.316467
java
你好 java2021-01-19T21:42:54.181102
</code>

Reference

RSC client: https://github.com/making/rsc/releases

JavaBackend DevelopmentSpring BootReactive Streamsnetwork protocolRSocket
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.