10

I'm kind of new to Reactive Stream, so I got a question when using Spring Webflux and Reactor.

I made a snippet like below:

@RestController
public class TestController {
    @GetMapping("responsebody/flux")
    public Flux<String> tt2() {
        return Flux.range(1, 5)
            .delayElements(Duration.ofMillis(1000))
            .map(l -> "hi");
    }
}

and, interestingly, the chrome shows the each element in the sequence separately, rather exposes all at a time when I request it just using browser. (But dev tools shows whole body at once)

But I'm wondering that, HOW IT WORKS when even HTTP 1 uses only one connection, and the data server sent is put in the body in HTTP protocol. HOW can the client know which separates each element and when the sequence completes? and what if client is not ready to use reactive stream?

I don't need any code using reactive library, but wanna know how the protocol works.

kerbermeister
  • 2,985
  • 3
  • 11
  • 30
dain.lee
  • 195
  • 10

1 Answers1

1

and what if client is not ready to use reactive stream?

Client has no idea about "reactive stream".

The behaviour you're wondering about is achieved with Chunked transfer encoding mechanism. When a client sends a request to the server, the server responses with header transfer-encoding: chunked

Client starts receiving the data in chunks. Data is sent in a series of chunks.

The Content-Length header is omitted in this case and at the beginning of each chunk added the length of the current chunk in hexadecimal format, followed by '\r\n' and then the chunk itself, followed by another '\r\n'. The terminating chunk is a regular chunk, with the exception that its length is zero. It is followed by the trailer, which consists of a (possibly empty) sequence of header fields.

kerbermeister
  • 2,985
  • 3
  • 11
  • 30