Observables and Subscriptions
Create and subscribe to observable streams.
Observables and Subscriptions is a free Angular Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is an Observable
An Observable is a lazy stream of values over time. Nothing happens until you subscribe. It can emit zero or more values, then either complete or error.
RxJS is the reactive library Angular ships for handling async work like HTTP, events, and timers.
Creating an Observable
The Observable constructor takes a subscriber function. You call next() to emit, complete() to finish, error() to fail.
import { Observable } from 'rxjs';
const numbers$ = new Observable<number>(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
subscriber.complete();
});Subscribing
Subscribing starts execution. Pass an observer with next, error, and complete handlers.
numbers$.subscribe({
next: v => console.log('value', v),
error: err => console.error(err),
complete: () => console.log('done')
});
// value 1, value 2, value 3, doneObservables are lazy
The subscriber function does not run until subscribe() is called. Each subscription triggers a fresh, independent execution (cold observable).
const tick$ = new Observable(s => {
console.log('executing');
s.next(Date.now());
});
// "executing" is NOT logged until subscribe() runsCreation helpers: of and from
You rarely use the constructor. Helpers like of and from create observables quickly.
import { of, from } from 'rxjs';
of(10, 20, 30).subscribe(v => console.log(v)); // 10 20 30
from([1, 2, 3]).subscribe(v => console.log(v)); // 1 2 3
from(fetch('/api')).subscribe(res => console.log(res));The interval and timer creators
interval emits incrementing numbers on a schedule; timer waits then emits.
import { interval, timer } from 'rxjs';
interval(1000).subscribe(n => console.log('tick', n));
// 0, 1, 2, ... every second
timer(2000).subscribe(() => console.log('after 2s'));HttpClient returns Observables
In Angular, HttpClient methods return Observables. The request fires only when you subscribe.
import { inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
const http = inject(HttpClient);
http.get('/api/users').subscribe(users => console.log(users));The Subscription object
subscribe() returns a Subscription. Call unsubscribe() to stop receiving values and free resources.
import { interval } from 'rxjs';
const sub = interval(1000).subscribe(n => console.log(n));
// later
sub.unsubscribe(); // stops the streamCompletion vs unsubscription
When an observable complete()s or error()s, it cleans up automatically. For infinite streams (interval, DOM events) you must unsubscribe yourself.
Observer shorthand
If you only care about values, pass a single function — it becomes the next handler.
of(1, 2, 3).subscribe(v => console.log(v));
// equivalent to { next: v => console.log(v) }Hot vs cold (brief)
Cold observables start producing on subscribe and each subscriber gets its own run (HTTP, of, interval). Hot observables produce regardless of subscribers and share values (we will meet Subjects next).
Quick Check
Test your understanding of observables.
Recap: Observables & Subscriptions
An Observable is a lazy stream you activate with subscribe().
- Emit with next, finish with complete, fail with error.
- HttpClient methods return observables — they fire on subscribe.
- Unsubscribe infinite streams to avoid leaks.
Next: Subjects and BehaviorSubject.
Frequently asked questions
Is the “Observables and Subscriptions” lesson free?
Yes — the full text of “Observables and Subscriptions” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.
What will I learn in “Observables and Subscriptions”?
Create and subscribe to observable streams. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Observables and Subscriptions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Observables and Subscriptions
- Subjects and BehaviorSubject
- Unsubscribing and Memory Leaks
- The async Pipe