Setup
- Angular 13.2.0
- Ngrx 13.0.2
- Backend: Spring Boot
Goal
I want to continually get the data from the backend. The data is changing and i want to reflect those changes in the angular-app.
What works so far
pollingfetchEntities$ = createEffect(
() => ({ scheduler = asyncScheduler, stopTimer = EMPTY } = {}) =>
this.actions$.pipe(
// Filter action type
ofType(DoctransActions.fetchEntities),
// Get the polling interval
switchMap(() =>
// Start polling
timer(0, 3000, scheduler).pipe(
// Stop the polling (used only in testing)
takeUntil(stopTimer),
switchMap(() =>
this.http.get<DoctransResponse>(`${this.apiUrl}/someurl`).pipe(
map(response => {
return action to set entities
}),
catchError((err) => {
return action for error
)
)
)
)
)
);
Question
Is there a better way for doing this? Maybe i am missing a complete other pattern?
Update 1
Investigating the websocket approach. From here: https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-websocket
it says
Latency alone is not a deciding factor. If the volume of messages is relatively low (for example, monitoring network failures) HTTP streaming or polling can provide an effective solution. It is the combination of low latency, high frequency, and high volume that make the best case for the use of WebSocket. Blockquote
The angular-app will run in the spring-boot-backend. So there wont be any latency. But i dont need real time data (in ms or s). 1 update every 30 sek or every min is good enough.
So maybe HTTP streaming or polling is enough? Dont know this yet. Anyone has some experience with it?
Solution
Solved with
- backend: spring webflux
- angular: EventSource