7 Rules for Effectively Using RxJS in Angular

Jan 18, 2023
header image

RxJS (Reactive Extensions for JavaScript) is a powerful library that allows developers to handle asynchronous data streams in a functional and efficient way.

In Angular, it is extensively used to handle data streams from various sources, such as user input, HTTP requests, and WebSockets. In this article, I will go over seven rules to follow for effective usage of RxJS in Angular.

1. Understand the basics of RxJS

Before diving into using RxJS in Angular, it's important to have a solid understanding of the basics of RxJS. This includes concepts such as Observables, Subjects, and Operators. Once you understand these concepts, it will be much easier to use RxJS in your Angular application.

Observables are the core concept of RxJS. They are a representation of a stream of data that can be emitted over time. An observable can emit multiple values, and can be subscribed to by one or more Observers, who will receive the emitted values. Observables can be created using the RxJS of, from, interval, fromEvent and ajax functions.

Subjects are a special type of Observable that can also be used to "push" values into the stream. They are useful for creating custom events or sharing data between multiple components. There are several types of subjects available in RxJS, including BehaviorSubject, ReplaySubject, and AsyncSubject.

Operators are functions that can be used to transform and manipulate the data in an observable stream. They are used to filter, map, and combine data streams, or to handle errors and complete notifications. They can be chained together using the pipe function, which makes it easy to read and understand the flow of data in your application. Examples of operators are map, filter, tap, concat, merge, switchMap, catchError and retry

2. Use the async pipe

The async pipe is a built-in Angular pipe that can be used to subscribe to an Observable and automatically unsubscribe when the component is destroyed. This can help prevent memory leaks in your application. The async pipe can also be used with subjects, promises and callback functions as well.

Note: Angular pipes are not to be confused with the Rxjs pipe, they are two different concepts.

import { Component } from '@angular/core'; import { Observable } from 'rxjs'; @Component({ selector: 'app-example', template: ` <div *ngIf="data$ | async as data; else loading"> {{ data }} </div> <ng-template #loading>Loading...</ng-template> ` }) export class ExampleComponent { data$: Observable<any>; constructor() { this.data$ = new Observable(observer => { setTimeout(() => { observer.next('Data Loaded'); }, 1000); }); } }

3. Use the takeUntil operator

The takeUntil operator is a useful operator in RxJS that can be used to unsubscribe from an Observable when a specific event occurs.

A common pattern is using the takeUntil operator to create a Subject that emits a value when a component is destroyed, and then use it to unsubscribe from an Observable when the component is destroyed.

import { Component, OnDestroy } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; @Component({ selector: 'app-example', template: `{{ data }}` }) export class ExampleComponent implements OnDestroy { data: string; private destroy$ = new Subject(); constructor() { const data$ = new Observable(observer => { setInterval(() => { observer.next('Data updated'); }, 1000); }); data$.pipe(takeUntil(this.destroy$)).subscribe(data => { this.data = data; }); } ngOnDestroy() { this.destroy$.next(); this.destroy$.complete(); } }

This pattern can also be implemented in a base class that can be inherited and reused by all components to cleanup subscriptions.

4. Use the shareReplay operator

The shareReplay operator can be used to share a single subscription to an Observable among multiple subscribers. This can help improve the performance of your application by reducing the number of HTTP requests made.

import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { shareReplay, tap } from 'rxjs/operators'; @Component({ selector: 'app-example', template: ` <div *ngFor="let item of data"> {{ item }} </div> ` }) export class ExampleComponent { data = []; constructor(private http: HttpClient) { this.http.get('https://mysimpleapi.com/posts') .pipe( tap(console.log), shareReplay(1) ) .subscribe(data => { this.data = data; }); } }

5. Use the catchError operator

The catchError operator can be used to handle errors that occur during the execution of an Observable. This can help you ensure that your application continues to function correctly even when errors occur.

import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { catchError } from 'rxjs/operators'; import { of } from 'rxjs'; @Component({ selector: 'app-example', template: `{{ data }}` }) export class ExampleComponent { data = ''; constructor(private http: HttpClient) { this.http.get('https://mysimpleapi.com/posts/100') .pipe( catchError(error => of("Error Occurred")) ) .subscribe(data => { this.data = data; }); } }

6. Use the pipe function

The pipe function can be used to chain multiple operators together, making it easier to read and understand the flow of data in your application. The pipe function takes one or more operators as arguments and returns a new Observable that has the operators applied to it.

import { Component } from '@angular/core'; import { of } from 'rxjs'; import { map, filter, tap } from 'rxjs/operators'; @Component({ selector: 'app-example', template: `{{ data }}` }) export class ExampleComponent { data = ''; constructor() { of(1, 2, 3, 4, 5) .pipe( filter(x => x % 2 === 0), map(x => x * 2), tap(console.log), ) .subscribe(x => this.data += x + ' '); } }

7. Practice and experimentation

The best way to become proficient with RxJS is through practise and experimentation. Try building small projects or experimenting with different operators to see how they work. This will help you become more comfortable using RxJS in your Angular applications.

Here are a few simple app ideas for anyone looking to get started right away:

Build a simple CRUD application: Create a simple application that allows you to perform basic CRUD operations on a list of items. Use RxJS to handle data streams, and experiment with different operators to see how they can be used to manipulate data.

Build a real-time chat application: Use WebSockets to create a real-time chat application that allows users to send and receive messages in real-time. Use RxJS to handle the WebSocket stream and experiment with different operators to filter and manipulate the data.

Handle errors with retry: Create a service that makes API calls and use the catchError operator to handle errors and the retry operator to retry failed requests. Remember to use shareReplay for the best performance.


RxJS is a powerful but very complicated tool, by understanding its basics, using built-in Angular features like the async pipe, and using operators like takeUntil, shareReplay, and catchError, you can effectively use RxJS in your Angular application to get the best performance and elegantly handle errors.