Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchEventSource() repeats request indefinitely #79

Open
Blabter opened this issue Apr 13, 2024 · 5 comments
Open

fetchEventSource() repeats request indefinitely #79

Blabter opened this issue Apr 13, 2024 · 5 comments

Comments

@Blabter
Copy link

Blabter commented Apr 13, 2024

I have SSE-endpoint http://localhost/sse-endpoint that returns next data:

data:{text: "He"}
id:0

data:{text: "ll"}
id:2

data:{text: "o,"}
id:4

data:{text: " u"}
id:6

data:{text: "se"}
id:8

data:{text: "r!"}
id:10

I use this client code to fetch data:

import { fetchEventSource } from "@microsoft/fetch-event-source";

console.log('Start');
fetchEventSource("http://localhost/sse-endpoint"), {
  method: "POST",
  onmessage(ev) {
    console.log('Received data');
    console.log(ev.data);
  }
});

Expected output:

Start
Received data
{text: "He"}
Received data
{text: "ll"}
Received data
{text: "o,"}
Received data
{text: " u"}
Received data
{text: "se"}
Received data
[{text: "r!"}

Actual output:

Start
Received data
{text: "He"}
Received data
{text: "ll"}
Received data
{text: "o,"}
Received data
{text: " u"}
Received data
{text: "se"}
Received data
[{text: "r!"}
Received data
{text: "He"}
Received data
{text: "ll"}
Received data
{text: "o,"}
Received data
{text: " u"}
Received data
{text: "se"}
Received data
[{text: "r!"}
// Output continues infinitely

In devtools tab "Network" I can see that requests to /sse-endpoint continues indefinitely

@sahilhaslani
Copy link

make sure your server code is responding with:

  res.setHeader('Cache-Control', 'no-cache, no-transform');
  res.setHeader('Connection', 'keep-alive');
  res.setHeader('Content-Type', 'text/event-stream');
  res.flushHeaders();

@Blabter
Copy link
Author

Blabter commented May 23, 2024

make sure your server code is responding with:

  res.setHeader('Cache-Control', 'no-cache, no-transform');
  res.setHeader('Connection', 'keep-alive');
  res.setHeader('Content-Type', 'text/event-stream');
  res.flushHeaders();

It's external server and I can't control it, but it has following HTTP response headers:

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: text/event-stream

@sameert89
Copy link

This will only happen when server keeps sending you events. On message only fires when server sends events. To mitigate this (since you don't have control over the server): call abort using an AbortController once you get a set of messages you need and reconnect when you need them again.

@ningxiner0201
Copy link

Set the "openWhenHidden" true and can be solved.
fetchEventSource(API, { openWhenHidden: true })

@congjinruo
Copy link

vConsole may cause this issue, remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants