Angular HttpClient: Observable with data continous data stream using interval requests to API endpoint
The following example requests /api/data
every second, creating an Observable
where you can subscribe to the continous value stream.
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { interval, mergeMap, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: HttpClient) { }
continousDataStream(_interval=1000): Observable<any> {
return interval(_interval).pipe(mergeMap(_ => {
return this.http.get<any>(`/api/data`);
}));
}
}