2

I am new to Angular and RxJS operators. I need help in one of the scenario in which I upload multiple documents at once in the backend (Rest API).

Processing the documents takes time in backend my my observable gets timeout in 2 minutes.

Now I am thinking of the solution in which I will send the post request to backend and will not wait for the response. And subscribe whenever response is ready by backend.

Please suggest if my approach is ok ? and how to achieve it in angular 8 with and Rxjs operators.

below is the my httpclient post method:

createNewVersion(formData: FormData) {
    let payload = new HttpParams();
    return this.httpClient
      .post(endpoint, formData, { params: payload }).subscribe((response: boolean) => {
        this.isLoading = false;
        if (response != null)
          this.observableVersion.next(true);
      });
  }

Currently I am facing timeout error after 2 minutes when response doesnt comes from backend that is why I have to change the logic.

Please help.

hunnyGeek
  • 31
  • 4
  • 2
    The timeout is most probably thrown by the [browser](https://stackoverflow.com/q/5798707/6513921). There isn't a way to control this browser timeout using RxJS as far as I know. You could try to increase the timeout using [`Keep-Alive`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive) header. If not a queuing mechanism needs to be configured in the back-end where it returns a response immediately upon the initial upload request and you could check back later if the upload is complete. – ruth Nov 03 '20 at 08:49
  • 2
    You could try a polling approach, where you send the data to the backend. The backend does not process the file immediately. Instead it queues the file to be processed and connects the file to an id. This id is returned and used for polling requests after. On server side there are workers which process the queued files. – MoxxiManagarm Nov 03 '20 at 08:57
  • @MichaelD How can I increase timeout using Keep Alive header ? Can you give some reference code for the same. – hunnyGeek Nov 03 '20 at 09:04
  • @user5281762: There's an example in the attached link. Note I haven't used it myself. So it's only a suggestion. – ruth Nov 03 '20 at 09:08
  • @MichaelD Keep Alive header is forbidden. https://fetch.spec.whatwg.org/#forbidden-header-name. Do you suggest any other solution? – hunnyGeek Nov 03 '20 at 10:41
  • The other suggestion would be what @MoxxiManagarm said. That'd be a better solution IMO. – ruth Nov 03 '20 at 11:18

0 Answers0