Search code examples
angularreactive-programmingspring-webflux

How to control Flux streaming interval with a specified value from UI


I have defined reactive end point in the following way ( sample):

    @CrossOrigin(origins = "*")
    @ApiOperation(value = "Streams AMQ related monitor data.")
    @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent> streamCustomerData() {
        return monitorService.createFluxForInterval("customers",
                gridService::fetchAllCustomers);
    }

    public Flux<ServerSentEvent> createFluxForInterval(ProcessName processName,
                Supplier method) {
            return Flux.interval(Duration.ZERO, Duration.ofSeconds(intervalPeriod))
                    .map(serverSentEvent -> ServerSentEvent.builder(method.get()).event(processName.description()).build());
        }
        

I have configured intervalPeriod in the yml but I want to control it from the UI using a dropdown. Can anyone please suggest And the angular code is in the following way

new Observable<any[]>(
  obs => {
    let eventSource = new EventSource("/customers");
    eventSource.addEventListener("customers", function (e: MessageEvent) {
      obs.next(JSON.parse(e.data));
    });
  });

Solution

  • I have passed intervalPeriod as path param and also closed the existing connection (eventSource.close()) and opened a new one whenever there is a change in the intervalPeriod as a browser can have max of 6 connections.