4

I don't have any code to show in this question. I am trying to google for it if there is someone who already created this feature with documentation. I also tried to find if someone already asked this question but didn't have that much luck.

This is similarly to file download and upload with progress bar but that only works on file uploads/downloads but how can I dot it in just basic any post/put/delete request to the backend.

So the end goal is I want to had a progress bar for each post and put or maybe delete request if possible that I will show the user the progress of their request with some percentage inside a progress bar with style will be display to the client side.

I know some developers already did this normally in big companies.

I know this will be done in JavaScript and some CSS and HTML or XML.

I found one question or issue with axios with the link of the issue or question right here.

But how can I do this in the Angular or Ionic way using http or httpClient service in the latest version.

Any suggestions?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
KnowledgeSeeker
  • 1,058
  • 2
  • 19
  • 44
  • 2
    I think this is what you are looking for: https://angular.io/guide/http#listening-to-progress-events – monogate Jul 03 '18 at 00:30
  • Thank you for this! I think this would help me. But the documentation didn't provide on how can I use this using promises? Any suggestion for that? – KnowledgeSeeker Jul 03 '18 at 00:47
  • In order to understand how to integrate this part, you should begin from the start of the doc flow: https://angular.io/guide/http – monogate Jul 03 '18 at 00:55
  • @monogate I finally used it but it only gives me the progress it's either `NaN` when offline and 100% on low end and high end connections even in using observables I need to track it from **0, 1, 2, 50 - 100%** I have a new question for this btw here is the [link](https://stackoverflow.com/questions/51146979/angular-4-httpclient-progress-event-how-to-track-each-percentage-in-runtime) – KnowledgeSeeker Jul 04 '18 at 23:44

1 Answers1

3

I have officially found a solution for this. After careful reading and understanding the use of http-events with the help of this article. I finally figured out that I was writing the write code I just didn't tested it very well because I didn't use file uploads so the http progress is so fast.

So here is a sample of what I did.

In my provider.ts

publishSomeAPI(data) {
    const req = new HttpRequest('POST', `${url}/someAPIEndpoint`, data, {
      // headers: You can put your headers type hear such as content/type, token...
      reportProgress: true
    });

    return new Promise((resolve, reject) => {
      this.http.request(req).subscribe((event: HttpEvent<any>) => {
        switch (event.type) {
          case HttpEventType.Sent:
            console.log('Request sent!');
            this.getHttpEvent().next(event);
            break;
          case HttpEventType.ResponseHeader:
            console.log('Response header received!');
            this.getHttpEvent().next(event);
            break;
          case HttpEventType.UploadProgress:
            this.getHttpEvent().next(event);
            const percentDone = Math.round(100 * event.loaded / event.total);
            console.log(`Posting in progress! ${percentDone}% \n
            Bytes being upload: ${event.loaded} \n
            Total no. of bytes to upload: ${event.total}`);
            break;
          case HttpEventType.Response:
            this.getHttpEvent().next(event);
            console.log('Done!', event.body);
            resolve(event);
        }
      }, err => {
        console.log(err);
        reject(err)
      });
    });
  }

I also created an observable which will observe and listen to the event that I had created above.

In top of my provider.ts file.

private httpEventListener: Subject<Object> = new Subject<Object>();
  observeHttp: Observable<Object> = this.httpEventListener.asObservable();

below.

  getHttpEvent(): Subject<Object> {
    return this.httpEventListener;
 }

After you created an observable in your service-provider.ts you can now start listening to that observable base on the http event that is executed or requested.

Now in your page.ts import the service or provider where you created the observable and put this code below in the constructor or whatever life cycle event you want to perform.

this.someService.observeHttp.subscribe(data => { console.log(`An http event happened: ${JSON.stringify(data, null, '\t')}`) })

Now you can do some state changes in the UI or perform another event base on that http event because you are now listening to the observable we created.

I found this ngx-loading-bar that can be useful for tracking events progress or loader.

NOTE: Use file uploads with simple text data instead of just small text data because small chunks of data without file will always return 100% when online or NaN with no connection even on low-end or mid-end devices.

KnowledgeSeeker
  • 1,058
  • 2
  • 19
  • 44