Skip to the content.

πŸ“˜ Angular > RxJS

πŸ—οΈ Understanding RxJS and Observables

RxJS (Reactive Extensions for JavaScript) is a library for reactive programming using Observables. It is a powerful tool for handling asynchronous data streams and event-based programming. RxJS is widely used in Angular applications to manage data streams, handle HTTP requests, and build reactive user interfaces.


πŸ“„ What are Observables?

An Observable is a core concept in RxJS. It represents a stream of data that can be observed over time. Observables emit data (or events) asynchronously, and you can subscribe to them to react to these emissions.

Key characteristics of Observables:


πŸ› οΈ Creating Observables

You can create Observables using the Observable constructor or RxJS creation functions. Common creation functions include:

Example:

import { of } from 'rxjs';

const numbers$ = of(1, 2, 3, 4, 5);

numbers$.subscribe(value => console.log(value));
// Output: 1, 2, 3, 4, 5

πŸ”„ Subscribing to Observables

To receive values from an Observable, you need to subscribe to it. The subscribe method takes up to three arguments:

Example:

import { from } from 'rxjs';

const array$ = from([10, 20, 30, 40]);

array$.subscribe(value => console.log(value));
// Output: 10, 20, 30, 40

βš™οΈ RxJS Operators

RxJS operators are functions that allow you to transform, filter, and combine Observables. Operators are applied using the pipe method.

Commonly Used Operators:


πŸ”— Piping Operators

Operators are applied to Observables using the pipe method. This allows you to chain multiple operators together in a readable and maintainable way.

Example:

import { of } from 'rxjs';
import { map, filter } from 'rxjs/operators';

const numbers$ = of(1, 2, 3, 4, 5);

numbers$
  .pipe(
    filter(value => value % 2 === 0), // Filter even numbers
    map(value => value * 10)         // Multiply each value by 10
  )
  .subscribe(value => console.log(value));
// Output: 20, 40

🌑️ Hot vs. Cold Observables


➿ Subjects and BehaviorSubjects

Example:

import { BehaviorSubject } from 'rxjs';

const subject = new BehaviorSubject('Initial Value');

subject.subscribe(value => console.log('Subscriber 1:', value));

subject.next('Updated Value');

subject.subscribe(value => console.log('Subscriber 2:', value));
// Output:
// Subscriber 1: Initial Value
// Subscriber 1: Updated Value
// Subscriber 2: Updated Value

πŸ”’ Unsubscribing from Observables

To prevent memory leaks, always unsubscribe from Observables when they are no longer needed. This is especially important for long-lived Observables like those tied to user interactions or component lifecycles.


🎯 Use Cases in Angular

RxJS and Observables are used extensively in Angular for:


πŸ”‘ Key Benefits


Mastering RxJS and Observables is essential for building robust, scalable, and maintainable Angular applications. Understanding these concepts will enable you to handle asynchronous data streams effectively and build reactive user interfaces.