I have problem with polling some data with the GET request from the API. I would like to poll data every 1 second, up to 30 seconds. The point is, that angular seems to be performing requests (it is logging response), while in fact it doesn't perform request to the server.
I wrote following methods in my service:
private pollStatus(token: string, remember: boolean):Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Token': token,
'Remember': '' + remember
})
};
const url = 'auth/status';
return this.http.get<any>(url, httpOptions).pipe(map(response => {
console.log('polldata', response.status);
return response;
}));
}
public secondFactor(token: string, remember: boolean): Observable<any> {
let pollData$ = this.pollStatus(token, remember);
let watchdog = timer(30 * 1000);
// this.http.post<any>('/auth/login', {}).subscribe();
return Observable.create(subject => {
let pollSubscription = pollData$.pipe(expand(_ => timer(1000).pipe(concatMap(_ => pollData$))), takeUntil(watchdog)).subscribe(response => {
console.log('secondFactor', response.status);
// some action based on the response is performed here
});
});
}
In the component I am calling it like that:
public ngOnInit(): void {
this.authService.secondFactor(this.authyToken, true).subscribe(response => {
console.log('response in component', response);
});
}
In the console I can see that, subscription of the get request is performed multiple times (code: console.log('polldata', response.status);
is executed). Unfortunately only one request is performed to the server (verified on the back-end and on the network tab).
Output in the console:
polldata pending
secondFactor pending
polldata pending
secondFactor pending
polldata pending
secondFactor pending
polldata pending
secondFactor pending
polldata pending
secondFactor pending
etc. etc. etc.
I checked this behavior under different browsers (Safari & Chrome) - same problem.
Work-around:
I found out, that if I will send some POST request to my server (commented line: // this.http.post<any>('/auth/login', {}).subscribe();
in the secondFactor()
method), then Angular start to perform GET requests more then once.