This guide will get you up and running with RxJS in minutes. You’ll learn how to create Observables, transform data with operators, and handle real-world scenarios.
Make sure you’ve installed RxJS before following this guide.
Let’s start with the simplest possible Observable that emits a sequence of values.
1
Create a new file
Create a file called app.ts (or app.js if not using TypeScript):
app.ts
Copy
Ask AI
import { of } from 'rxjs';// Create an Observable that emits three valuesconst numbers$ = of(10, 20, 30);// Subscribe to receive the valuesnumbers$.subscribe({ next: value => console.log('Received:', value), error: err => console.error('Error:', err), complete: () => console.log('Complete!')});
The $ suffix is a common naming convention for Observables, making them easy to identify in your code.
2
Run your code
Execute your file:
Copy
Ask AI
node app.js
3
See the output
You should see:
Copy
Ask AI
Received: 10Received: 20Received: 30Complete!
Congratulations! You’ve created your first Observable.
Let’s create a timer that emits values every second:
timer.ts
Copy
Ask AI
import { interval, take } from 'rxjs';// Emit a value every 1000ms (1 second)const timer$ = interval(1000);// Take only the first 5 valuesconst limitedTimer$ = timer$.pipe(take(5));limitedTimer$.subscribe({ next: value => console.log(`Tick ${value}`), complete: () => console.log('Timer finished!')});// Output:// Tick 0// Tick 1// Tick 2// Tick 3// Tick 4// Timer finished!
The interval function creates an Observable that emits sequential numbers starting from 0. The take operator limits the number of emissions.
Deep dive into Observables, Observers, and Subscriptions
Operators Guide
Master the full suite of RxJS operators
Subjects
Learn about multicasting with Subjects
Schedulers
Control timing and concurrency
Pro tip: The best way to learn RxJS is by building! Try recreating common UI patterns like autocomplete, infinite scroll, or drag-and-drop using Observables.