Farfetch'd
Farfetch'd

Reputation: 152

Can't subscribe to WebFlux stream in Angular

I wrote a server that produces stream thanks to Flux from WebFlux (Spring boot). The method that provid stream

public Flux<Data> getStream() {

        Flux<Data> initialData = Flux.just(this.data);

        Flux<Data> periodicData = Flux.interval(Duration.ofSeconds(60))
                .map(tick -> this.data);

        return Flux.concat(initialData, periodicData);
}

I have also defined a request handler :


public Mono<ServerResponse> streamData(ServerRequest request) {
        return ServerResponse.ok()
                .contentType(MediaType.TEXT_EVENT_STREAM)
                .body(DataService.getStream(), Data.class);
}

And a request router:

RouterFunction<?> routes (RequestHandler requestHandler) {
        return RouterFunctions
                .route(RequestPredicates
                        .GET("/data"),
                        requestHandler::streamData);
}

When I'm using postman I could easly subscribe to this stream enter image description here

But when I'm trying to subscribe to this stream with my angular client I got this enter image description here

Here, my Webflux client in angular

getStream() {
    return this.http.get<Data>(this.apiUrl+'/data', {observe : 'events'}).subscribe(
      (event : HttpEvent<Data>) => {
        if (event.type === HttpEventType.Response){
          console.log('Message : ', event.body);
        }
      }
    );
  }

Please help me to fix this issue

Upvotes: 1

Views: 462

Answers (1)

Hager Khaled
Hager Khaled

Reputation: 84

you need to specify response type to text

this.http.get<Data>(this.apiUrl+'/data', {observe : 'events', responseType: 'text'})

Upvotes: 0

Related Questions