EventSource: .close() does not abort current event source fetch connections

Fetch supports abort, if it is not provided or called it leaves long leaving TCP connections open. Abort package : https://www.npmjs.com/package/abortcontroller-polyfill

Example code/changes that could make this work:

  1. Create a controller as described in https://www.npmjs.com/package/abortcontroller-polyfill

  2. Add the abort signal to the call of transport

  transport.open(xhr, onStart, onProgress, onFinish, requestURL, withCredentials, requestHeaders, controller.signal);
  1. Store the controller in order to be able to access it in close
    es.controller = controller; 
  1. Update close
  EventSourcePolyfill.prototype.close = function () {
    this.controller.abort();
    this._close();
  };
  1. Update the definition of transport when the browser supports fetch
  FetchTransport.prototype.open = function (xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers, signal) {
    // cache: "no-store"
    // https://bugs.chromium.org/p/chromium/issues/detail?id=453190
    var textDecoder = new TextDecoder();
    fetch(url, {
      headers: headers,
      credentials: withCredentials ? "include" : "same-origin",
      signal: signal
    })

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 2
  • Comments: 20 (11 by maintainers)

Most upvoted comments

any updates on this?

Thanks a lot! I will be able to test it next week

try to use the Transport option:

var es = new EventSource(url, {
  Transport: XMLHttpRequest
});

I see no solutions with Fetch API.