How to call a web service every x seconds?

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


Use interval to call a web service every x second.

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

1 Like

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


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.


What about calling a web service every x seconds even if the app runs in background?

I have to send the position of a device to the server every 15 seconds.
I’m using Background Geolocation to get the position of the device even if the app is in background.

But if I use this code to send the position every 15 seconds:

    pageObject = this;
    INTERVAL_MS = 15000;

    this.timerSendDataToServer = setInterval(this.sendGeolocationDataToServer, INTERVAL_MS,
      deviceId, pageObject);

the web service is not called at regular intervals.
Some times is called every 15 seconds, some times after 2 minutes, a few times is called twice within a few seconds.

sendGeolocationDataToServer is the function that calls the web service.

So, is there a way to call a web service every 15 seconds even if the app runs in background?

Thank you very much


I’m using this code to start the job:

  startScheduling2() {
    this.showSpinner2 =  true;
    this.scheduling2Started =  true;
    this.scheduling = interval(TIME_INTERVAL_MS);
    const subscribe = this.scheduling.subscribe(val => this.sendGeolocationDataToServer(val));

How can I stop the job?
I tried
but doesn’t work.

The error is:

ScheduledJobsPage.html:37 ERROR TypeError: this.scheduling.unsubscribe is not a function
    at ScheduledJobsPage.stopScheduling2 (
    at Object.eval [as handleEvent] (ScheduledJobsPage.html:38)
    at handleEvent (core.js:38098)
    at callWithDebugContext (core.js:39716)
    at Object.debugHandleEvent [as handleEvent] (core.js:39352)
    at dispatchEvent (core.js:25818)
    at core.js:37030
    at HTMLElement.<anonymous> (platform-browser.js:1789)
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:34182)


One way to think about reactive programming is that observables are performers, and observers are audience members.

So scheduling is your performer, and subscribe is an audience member. The act of unsubscribing constitutes the audience member leaving the concert; it’s not something a performer does. So unsubscribe has to be called on the Subscription object - the thing you have currently named subscribe (but I would change that name to a noun like subscription).


Hi Claudio, did you solve this problem? I have the same problem
Thanks in advance!