0

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.

  • Do you observe same behavior when connecting directly to microservice not going through the gateway? – Gaël Marziou Nov 14 '22 at 20:35
  • I have the same behavior. No data, then all the data. – ZiebaTomasz Nov 14 '22 at 20:50
  • Did you reproduce using localhost to avoid networking equipments buffering? Could be also due to webpack dev server that you use to run your frontend. Have you tried in a production environment? Have you read this https://stackoverflow.com/questions/50870716/why-are-my-server-sent-events-arriving-as-a-batch – Gaël Marziou Nov 15 '22 at 12:11

0 Answers0