fetch-event-source icon indicating copy to clipboard operation
fetch-event-source copied to clipboard

fetchEventSource() repeats request indefinitely

Open Blabter opened this issue 1 year ago • 6 comments
trafficstars

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

Blabter avatar Apr 13 '24 00:04 Blabter