RxJS Basics: Observables and Subscriptions

Angular 15 min min read Updated: Nov 16, 2026 Intermediate
RxJS Basics: Observables and Subscriptions
Intermediate Topic 2 of 5

What Is an Observable?

An Observable is a stream of values over time. Think of a YouTube channel: you subscribe and you receive every new video until you unsubscribe. RxJS is the library that provides observables and many tools to combine them. Angular uses it for HTTP, router events, forms and more.

PromiseObservable
ValuesOneZero, one or many
StartsAt onceWhen you subscribe
CancelNoYes (unsubscribe)
Operatorsthen onlyMany (map, filter, switchMap)

Create and Subscribe

typescript import { Observable, of, from, interval, fromEvent, timer } from 'rxjs'; const numbers$ = of(1, 2, 3); // emits 1, 2, 3 then completes const list$ = from([10, 20, 30]); // from an array or promise const ticks$ = interval(1000); // 0, 1, 2 ... every second, forever numbers$.subscribe({ next: value => console.log('value', value), error: err => console.error('error', err), complete: () => console.log('done'), });
The $ suffix: By convention, variables holding observables end with $ (users$). It reminds you to subscribe or use async.

Custom Observable

typescript const countdown$ = new Observable<number>(subscriber => { let n = 3; const id = setInterval(() => { subscriber.next(n--); if (n < 0) { subscriber.complete(); clearInterval(id); } }, 1000); return () => clearInterval(id); // teardown when unsubscribed }); const sub = countdown$.subscribe(v => console.log(v)); // later sub.unsubscribe();

Always End Subscriptions

WayExample
async pipe{{ data$ | async }} (automatic)
toSignal()data = toSignal(data$) (automatic)
takeUntilDestroyed().pipe(takeUntilDestroyed()) in constructor
Finite observablesHTTP requests complete by themselves
Manualconst s = x$.subscribe(...) then s.unsubscribe() in ngOnDestroy

Subjects

A Subject is an observable you can push values into yourself. It is useful for events and for services that share data.

typescript import { Subject, BehaviorSubject, ReplaySubject } from 'rxjs'; const clicks$ = new Subject<string>(); clicks$.subscribe(v => console.log('A', v)); clicks$.next('first'); // A first const user$ = new BehaviorSubject<User | null>(null); // always has a current value user$.subscribe(u => console.log('now', u)); // immediately logs null user$.next({ id: 1, name: 'Ana' }); console.log(user$.value); // current value
TypeNew subscribers get
SubjectOnly future values
BehaviorSubject(initial)The latest value immediately
ReplaySubject(n)The last n values
AsyncSubjectThe last value, only after completion

A Service with a Subject

typescript @Injectable({ providedIn: 'root' }) export class NotificationService { private message$ = new Subject<string>(); readonly messages$ = this.message$.asObservable(); // hide next() from others show(text: string) { this.message$.next(text); } }

Cold and Hot

  • Cold: each subscriber starts its own run (an HTTP request is sent for every subscriber)
  • Hot: values are produced regardless of subscribers (clicks, a Subject)
  • shareReplay(1) turns a cold observable into one that shares a single run
Observables or signals? Use signals for state shown in the UI. Use observables for streams of events and for asynchronous work such as HTTP, debounced input and websockets. You can convert between them (see the interop lesson).

Conclusion

Observables are streams you subscribe to. Learn subscribe, Subject, BehaviorSubject and always make sure subscriptions end with async, toSignal or takeUntilDestroyed.

Quick Summary: Observable = stream; subscribe to start; Subject pushes values; always clean up.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators