Observables and Subscriptions
Create and subscribe to observable streams.
Observables and Subscriptions is a free Angular Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.
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.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 38
- Lessons
- 144
Frequently Asked Questions
Is the “Observables and Subscriptions” lesson free?
Yes — any 3 lessons of the Angular Academy learning path, including “Observables and Subscriptions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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