SSE events are received when the connection is closed in JHIpster and Gateway

78 Views Asked by At

I have a simple code in Jhipster microservice which sends words periodically:

@GetMapping(path =  "/words", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
SseEmitter getWords() {
    List<String> dataSets = Arrays.asList("one ", "two", "three", "four", "five");
    SseEmitter emitter = new SseEmitter();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        try {
            for (String dataSet : dataSets) {
                randomDelay();
                emitter.send(SseEmitter.event().name("update").id("1").data(dataSet));
            }
            emitter.complete();
        } catch (IOException e) {
            emitter.completeWithError(e);
        }
    });
    executor.shutdown();
    return emitter;
}

In the frontent I use:

sse = new EventSource(
  `${serverURL}/services/news/api/words&access_token=` + (localStorage.getItem('jhi-authenticationToken') || sessionStorage.getItem('jhi-authenticationToken')), {});

sse.onmessage = (event) => {
  console.log('event', event)
  const data = event.data;
}

Instead of having message by message every 1 second I receive all the messages at once after 5 seconds. How to make it works periodically and receive message by message? BTW I use reactive Gateway and non-reactive microservice for the SSE.

0

There are 0 best solutions below