How to call a web service every x seconds?

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

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);

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.

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

2 Likes

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

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

cld

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
this.scheduling.unsubscribe();
but doesn’t work.

The error is:

ScheduledJobsPage.html:37 ERROR TypeError: this.scheduling.unsubscribe is not a function
    at ScheduledJobsPage.stopScheduling2 (scheduled-jobs.page.ts:56)
    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)

cld

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).

3 Likes

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