Polling with a NgRx Effect

Tim Deschryver
Tim Deschryver
timdeschryver.dev

Polling with a NgRx Effect

Use case

You want to periodically the refresh data in the NgRx Store.

Solution

Create a NgRx Effect that retrieves the data via a service every x minutes, this can be done with the RxJS timer operator.

refresh$ = createEffect(() => {
// every 10 minutes
return timer(0, 600000).pipe(
switchMap(() =>
this.customersService.get().pipe(
map((data) => refreshSuccess(data)),
catchError((response) => refreshFailed(response)),
),
),
);
});

Feel free to update this blog post on GitHub, thanks in advance!

Enjoying the blog?

Support my work

If you enjoyed this post and found it useful, consider supporting my work. It helps me keep creating and sharing content like this. Thank you!