Jose L. Garcia
Jose L. Garcia

Reputation: 336

Ionic Capacitor strategy for http requests

I've an application with Capacitor 3, Ionic 5 and Angular. I'm using Capacitor Http plugin for all requests due to CORS problem in the server.

The application makes http requests against the server, but sometimes, when the quality of network is bad, the request hangs the device or fails and exit the app.

I can't implement a retry strategy, for example with Observables in angular, because I'm using the Capacitor Http plugin and not Angular HttpClient.

With Capacitor I can detect if the device is online (network connectivity) or it's offline, but not a poor connection.

Does anyone have experience with this situation and how to resolve it?

Regards J

Upvotes: 0

Views: 2034

Answers (3)

you can use angular http but your server is restricted so angular http not working so you can create proxy server in herokuapp and you also use angular http(see this video https://www.youtube.com/watch?v=C0GSRPeuDAM)

Upvotes: 0

simonramosb
simonramosb

Reputation: 71

you can just switch to Angular HttpClient! If that doesn't help, you can check the connection with capacitor's network plugin in every page, calling it in the app-component.ts

Upvotes: 0

Emre Akbaki
Emre Akbaki

Reputation: 276

I think you can try few things maybe its can help you for solve the problem.

  • Test connection on every page change.(You can write a global service for control the network connection. Every page opening you control
    the connection with ionViewWillEnter() so if the connection bad show the alert.

  • Convert Capacitor HttpClient to Angular HttpClient

  • Maybe you can try setTimeout function. Call the function before the http request and then control the response. If you don't have any response fail the request and show alert.

I hope these help you.

Upvotes: 1

Related Questions