Coder Social home page Coder Social logo

Comments (11)

pociej avatar pociej commented on June 4, 2024 8

@ChepteaCatalin this doesnt work. Im not sure yet what the solution is but I checked above and after controller.abort() call messages are still there. onmessage callback is still firing.

from fetch-event-source.

Systemcluster avatar Systemcluster commented on June 4, 2024 8

Calling controller.abort() in onerror can't cancel retries with this library.

onerror is called here, after which the retry timer will be reset when it doesn't throw, without checking for aborted again:

if (!curRequestController.signal.aborted) {
// if we haven't aborted the request ourselves:
try {
// check if we need to retry:
const interval: any = onerror?.(err) ?? retryInterval;
window.clearTimeout(retryTimer);
retryTimer = window.setTimeout(create, interval);
} catch (innerErr) {

The retry timer will call create where a new AbortController is created that starts non-aborted:

async function create() {
curRequestController = new AbortController();
try {

Retries will be stopped if controller.abort() was called in onopen, onmessage or onclose, but calls in onerror won't affect it.

Throwing in onerror is absolutely the correct thing to do to stop the request, which is also shown in the readme:

onerror(err) {
if (err instanceof FatalError) {
throw err; // rethrow to stop the operation
} else {

from fetch-event-source.

nickscamara avatar nickscamara commented on June 4, 2024 7

For those having the issue in React, this works:

const [ abortController, setAbortController ] = useState(new AbortController());

fetch(
   signal: abortController.signal
   // ...
)

function stopResponseSSE() {
    abortController.abort();
    setAbortController(new AbortController());
}

As June mentioned, "once an instance of AbortController is used, we need to create a new instance to reset the value."

In React, you most likely will need to set the state with a new Abort Controller.

Hope this helps!

from fetch-event-source.

ChepteaCatalin avatar ChepteaCatalin commented on June 4, 2024 6
const controller = new AbortController()
const signal = controller.signal

pass signal to the object from the second parameter of fetchEventSource()

When you want to close the connection, just call controller.abort()

https://stackoverflow.com/questions/31061838/how-do-i-cancel-an-http-fetch-request

from fetch-event-source.

JuneDan avatar JuneDan commented on June 4, 2024 6

Once an instance of AbortController is used, we need to create a new instance to reset the value.
eg:

function stopResponseSSE() {
    ctrl.abort()
    ctrl = new AbortController();
}

from fetch-event-source.

jchiare avatar jchiare commented on June 4, 2024 5

Curious about this too. Aborting the fetch request didn't seem to work for me. Workaround I use is to throw an error in the onerror and onclose functions

        onclose() {
          // do something
          throw new Error(); // hack to close the connection
        },
        onerror() {
          // do something
          throw new Error(); // hack to close the connection

from fetch-event-source.

xiaohundun avatar xiaohundun commented on June 4, 2024 3

@ChepteaCatalin this doesnt work. Im not sure yet what the solution is but I checked above and after controller.abort() call messages are still there. onmessage callback is still firing.

Make sure your controller or signal are the same. Especially in React.

from fetch-event-source.

lucrus73 avatar lucrus73 commented on June 4, 2024 1

its easy to do in modern browsers follow this.

Yes, it's easy and it's just as documented, except it doesn't close the connection, at least not in my case.

from fetch-event-source.

Codefa avatar Codefa commented on June 4, 2024

its easy to do in modern browsers follow this.

 const controller = new AbortController()
 const { signal } = controller

 await fetchEventSource('your API', {
	onmessage(ev) {
	  console.log('data', ev.data)
	},
	onclose() {
         controller.abort()
	 // do something
       },
       onerror() {
	// do something
       },
       signal,
   })

and When you want to close the connection, just call controller.abort()

from fetch-event-source.

bsaphier avatar bsaphier commented on June 4, 2024

For those having the issue in React, this works:

const [ abortController, setAbortController ] = useState(new AbortController());

fetch(
   signal: abortController.signal
   // ...
)

function stopResponseSSE() {
    abortController.abort();
    setAbortController(new AbortController());
}

As June mentioned, "once an instance of AbortController is used, we need to create a new instance to reset the value."

In React, you most likely will need to set the state with a new Abort Controller.

Hope this helps!

Better to use a useRef to store the abortController instead of useState as you'll end up triggering a (likely undesired) re-render when you call setAbortController(new AbortController());

from fetch-event-source.

rzec-allma avatar rzec-allma commented on June 4, 2024

As @Systemcluster said, the only thing that works to stop the retry functionality is to throw an error in the onerror callback.

If this is the correct thing to do, it would probably help to add a small section to the readme about this (I would create a PR however I am not sure if this is the expected way to handle this cause or if throwing an error just happens to do what I want).

from fetch-event-source.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.