Yaffle / EventSource

a polyfill for http://www.w3.org/TR/eventsource/
MIT License
2.11k stars 338 forks source link

Works in Postman but not in UI #225

Open Sritharan-racap opened 8 months ago

Sritharan-racap commented 8 months ago

Hi I'm using EventSource for making SSE call to my Java backend to build internal ChatGPT like application. Java Code below:

    @GET
    @Path("/chat-stream")
    @Produces("text/event-stream")
    @Timed(absolute = true, name = "GET./chat-stream")
    // @formatter:on
    public void getChatStream(@Context SseEventSink sseEventSink, @Context Sse sse,
            @BeanParam AiChatRequestFilter filter) {
        executor.execute(() -> {
            int count = 10;
            while (count > 0) {
                try {
                    Thread.sleep(500);
                    sseEventSink.send(sse.newEvent("message", "Hello World -    " + count));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count--;
            }
            sseEventSink.close();
        });
    }

The above code works in Postman, i.e when i make the api call, i get the message registered right away after 500ms. But when I do the same call via UI, the message is not logged right away after 500ms. It waits for the connection to complete (i.e after 5s, i get all the message logged in the console)

UI code attached below:

sseTest() {
    const authToken = this.localStorageService.get('tokenInfo')['accessToken'];
    const eventSourceInitDict = {
      headers: {
        Authorization: 'Bearer ' + authToken
      }
    };
    let url = '/api/ai/notes/chat-stream';
    url += '?prompt=' + encodeURIComponent('How is Macrogenics addressing the toxicity issues seen with Vobra Duo in the dose expansion cohort?');
    url += '&domainName=' + encodeURIComponent('AML_OVN_2021');
    const es = new EventSourcePolyfill(url, eventSourceInitDict);
    const listener = function(event) {
      console.log(event);
    };
    es.addEventListener('open', listener);
    es.addEventListener('message', (event) => {
        console.log(event);
    });
    es.addEventListener('error', listener);
  }

And the call is getting repeated in an infinite loop even if i'm calling the function only once.That's a weird part as well.

Any help on this will be greatly appreciated pls ?

Thanks