How to call a web service every x seconds?


#1

Hi,
I have to monitor the state of a device every x seconds.
To do this, I have to call a web service that returns the state of the device periodically.
Which is the smartest way to do this?

The check should be done while the user is using the app.
Should I implement a sort of background service?
Is it possible with Ionic4?

Thank you very much

Claudio


#2

Use interval to call a web service every x second.

//Interval is use to refresh/timeout page, 10000 = 10s
setInterval(data => {
console.log(“test”);
// this.ReadSMSList();
}, 10000);


#3

I would probably use RxJs’ interval method. This would give me more options for the response than just a standard JS interval call.

https://www.learnrxjs.io/operators/creation/interval.html


#4

Thank you @ChrisGriiffith Chris and @gokujy ,
I think that the first method stops to work if the user changes the page.
However I’ll try both and I’ll let you know the result.
Thank you.

cld