liveBook · Manning

Request-stream model

As explained in Spring in Action, Sixth Edition

The request-stream model is a communication pattern in which a client sends one request and a server returns a stream of zero or more values.

The request-stream model lets a client send one request and receive a stream of zero or more values. RSocket represents the request with Mono and the response with Flux, supporting asynchronous, nonblocking reactive communication. section 14.1

A Spring implementation follows this shape:

  • Define a value object for each streamed result, such as a stock quote containing a symbol, price, and timestamp. section 14.2.2
  • Map a route such as stock/{symbol} with @MessageMapping and capture the symbol using @DestinationVariable. section 14.2.2
  • Return Flux<StockQuote>; Spring interprets the Flux as a request-stream interaction. Create values periodically with Flux.interval and map each interval to a result. section 14.2.2
  • On the client, route the request through RSocketRequester, call retrieveFlux rather than retrieveMono, subscribe, and process each value. section 14.2.2

Streaming stock quotes

A client requests the price for XYZ once, then receives a new randomly generated StockQuote every second. This avoids repeatedly polling for the current price. section 14.2.2

Figure 14.2 RSocket’s request-stream communication model

Visualizes the single request followed by a stream of response values. section 14.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains the model, controller and client changes, and periodic Flux construction without descending into protocol internals.
Examples
Uses a Spring stock-quote controller that emits randomly priced StockQuote objects every second to a subscribed client.
What it assumes
Assumes familiarity with reactive types such as Mono and Flux while showing the request-stream distinction from request-response.
What you can do after
Apply it via Spring controllers and RSocketRequester to implement and consume periodic reactive streams.
How it connects
Connects RSocket with Reactive Streams, Mono, Flux, request-response, fire-and-forget, channel, and RSocketRequester.

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.