Loopback 4: How to abort a request

58 Views Asked by At

I have a long streaming request. On the client side I am using fetch with a signal provided like this:

controller = new AbortController();
const signal = controller.signal;
  signal.addEventListener("abort", () => {
  console.log("ABORTED");
});

const response = await fetch(`.../stream`, {
  method: "POST",
  signal,
  headers: {
    "Content-Type": "application/json",
     Authorization: `Bearer ${authStore.user.token}`,
  },
  body: JSON.stringify(request),
});

And on the backend, in loopback I have a controller and inject a Request like this... listening to abort or close events but it never stops...

@post('.../stream', ...)
async createStream(@requestBody(...), @inject(RestBindings.Http.REQUEST) request: Request) {

  request.on('close', () => {
    console.log('Request closed during processing');
  });
      
  request.on('abort', () => {
    console.log('Request closed during processing');
  });
  

  // some logic
  ...
}

While executing the FETCH stream from the client side I am getting the data, when I execute the controller.abort(), nothing really happens. The request does not start producing. I don't get any logs in the console on the server side, nor do I get the I don't get the ABORTED on the client side.

What am I doing wrong?

0

There are 0 best solutions below