I am looking to incorporate polling into my Nativescript with Angular application. I want to continuously check the status of my backend server every second. Currently, I have a service set up as follows:
@Injectable()
export class StateService {
getStatus(): Observable<StateApp> {
let headers = this.getHeaders();
return this.http
.get(BackendService.stateUrl, {
headers: headers
})
.map(res => {
return res.json() as StateApp
})
.catch(this.handleErrors);
}
Next, I would like to subscribe to this service and send requests to the server. In my component, I have written the following code:
IntervalObservable.create(1000).subscribe(() => {
this.statusService.getStatus()
.subscribe(
(next) => {
this.state = next;
};
});
I attempted to implement a solution I found on Stack Overflow from nearly two years ago that addresses a similar issue (Angular2 http at an interval). However, certain methods mentioned in the post, such as IntervalObservable.takeWhile, no longer exist in rxjs version 5.4.3. So, I am curious about the best way to achieve the same result (i.e., getting the initial result without waiting one second and stopping requests when the component is destroyed).