← Learning Hub
Advanced

RxJS Basics

An Observable is a stream of values over time: keystrokes, HTTP answers, timer ticks. Angular's HttpClient, router and forms use RxJS Observables. You subscribe to get the values, and use operators to shape the stream.

An Observable is like a water pipe: values flow through it over time, and operators are fittings that filter, transform or combine the flow. Many things in a web app are naturally streams - keystrokes, clicks, server messages, timers - and RxJS gives you a powerful toolkit for them. You don't need to master all of RxJS; a handful of operators covers most real needs.

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

of(499, 1299, 199)
  .pipe(
    filter(p => p < 1000),
    map(p => Math.round(p * 1.18))
  )
  .subscribe(v => console.log(v));
// 589
// 235

interval(1000).pipe(take(3)).subscribe(n => console.log('tick', n));
// tick 0, tick 1, tick 2 (one per second)

Search as you type

This classic pattern waits until the user stops typing, skips repeats, cancels old requests and keeps only the newest answer.

Example
import { Component, inject } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { toSignal } from '@angular/core/rxjs-interop';
import { debounceTime, distinctUntilChanged, switchMap, of } from 'rxjs';

@Component({
  selector: 'app-search',
  imports: [ReactiveFormsModule],
  template: `
    <input [formControl]="query" placeholder="Search products">
    @for (p of results(); track p.id) { <p>{{ p.title }}</p> }
  `,
})
export class Search {
  private http = inject(HttpClient);
  query = new FormControl('', { nonNullable: true });

  results = toSignal(
    this.query.valueChanges.pipe(
      debounceTime(300),                   // wait for a 300 ms pause
      distinctUntilChanged(),              // ignore the same text twice
      switchMap(q => q.length < 2
        ? of([])
        : this.http.get<{ id: number; title: string }[]>(`/api/search?q=${encodeURIComponent(q)}`))
    ),
    { initialValue: [] }
  );
}

Must-know operators

  • map, filter - change or drop values
  • debounceTime - wait for a pause
  • switchMap - start a new inner request and cancel the old one
  • catchError - recover from an error
  • takeUntilDestroyed() - stop listening automatically when the component is destroyed
Signals or Observables? Use signals for state shown on screen. Use Observables for events over time (typing, websockets, HTTP). Convert with toSignal() and toObservable().
Key points
  • Observables are streams of values over time.
  • Operators like map, filter, debounceTime and switchMap shape the stream.
  • Use signals for screen state, Observables for events over time.

📝 Test yourself

1. Which operator waits until the user stops typing?
2. Why use switchMap for search requests?
3. Fill in the method you call to start receiving values:
numbers$.(n => console.log(n));