Angular HttpClient: Observable mit kontinuierlichem Datenstrom über Intervall-Anfragen an API-Endpunkt
Das folgende Beispiel fragt jede Sekunde /api/data ab und erstellt ein Observable, bei dem Sie den kontinuierlichen Wertstrom abonnieren können.
data_service.ts
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`);
}));
}
}Check out similar posts by category:
Angular
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow