3

I've created a microservice that returns a response in form of a stream using StreamingResponseBody class. I want to consume this response in AJAX call using RxJS Observables in my Angular 4 application. Here is my controller:

@RestController
public class MyController {
    @RequestMapping(value = "/api/oneToHundred", method = RequestMethod.GET)
    public StreamingResponseBody oneToHundred() {
        return new StreamingResponseBody() {
            @Override
            public void writeTo(OutputStream out) throws IOException {
                for (int i=1;i<=100;i++) {
                    out.write((i + "\n").getBytes());
                    out.flush();
                }

            }
        };
    }
}

Needless to say that there will be partial results before the request is entirely completed. I want to update the UI as soon as I'm getting a partial result, say display each partial result in as a list item on my HTML page like follows:

  • 1
  • 2...

after next set of partial response

  • 1
  • 2
  • 3
  • 4...

after next set of partial response

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9......

upto

  • ...
  • 98
  • 99
  • 100

Is it possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Did you find a way to do it? – Abdennacer Lachiheb Nov 18 '18 at 11:47
  • I did actually, using fetch and ReadableStream.getReader(). refer to 1. https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream and 2. https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader upon receiving each bundle of data from the back-end, we can handle them individually. It can be used in a variety of ways to improve site performance. – user3374764 Dec 19 '18 at 11:18

0 Answers0