Angular RxJS
Learn the fundamentals of RxJS and understand how RxJS works with Observables, streams and operators in Angular applications.
What is RxJS?
RxJS stands for Reactive Extensions for JavaScript. It is a library for reactive programming using Observables and other stream-processing features.
RxJS helps developers work with values that arrive over time and provides functions for creating, transforming, filtering, combining and controlling those streams.
Angular applications commonly use RxJS when working with asynchronous data and Observable-based APIs.
How RxJS Works
Core RxJS Concepts
Observable
Represents a stream of values over time.
Subscriber
Consumes values emitted by an Observable.
pipe()
Connects RxJS operators into a processing pipeline.
Operators
Transform, filter and control Observable streams.
Importing RxJS
import {
Observable,
of,
map,
filter
} from '@rxjs';
RxJS Observable Example
RxJS can be used to create an Observable that emits values to a subscriber.
const numbers$ =
of(10, 20, 30);
numbers$.subscribe({
next: value => {
console.log(value);
},
complete: () => {
console.log(
'Completed'
);
}
});
Output
10
20
30
Completed
Using pipe() in RxJS
The pipe() method allows multiple RxJS operators to be applied to an Observable in sequence.
Each operator processes the stream and passes its result to the next stage.
const numbers$ =
of(1, 2, 3, 4, 5);
const result$ = numbers$.pipe(
map(value => value * 10),
filter(value => value > 20)
);
Result
30
40
50
RxJS Processing Pipeline
RxJS vs Observable
| Concept | Meaning | Example |
|---|---|---|
| RxJS | Reactive programming library | RxJS APIs |
| Observable | Represents a stream of values | Observable<number> |
| Operator | Processes an Observable stream | map(), filter() |
| Subscription | Represents active Observable execution | subscribe() |
RxJS Creation Functions
of()
Creates an Observable from supplied values.
from()
Creates a stream from supported input sources.
interval()
Emits sequential values at regular intervals.
timer()
Creates time-based Observable sequences.
Transforming RxJS Data
RxJS operators can transform data before it reaches the subscriber.
For example, map() can convert each emitted value into another form.
const courses$ = of(
'Java',
'.NET',
'Python'
);
const courseNames$ =
courses$.pipe(
map(course =>
course + ' Full Stack'
)
);
courseNames$.subscribe({
next: course => {
console.log(course);
}
});
Filtering RxJS Data
The filter() operator allows only values that satisfy a condition to continue through the stream.
This is useful when the application needs only a subset of the incoming values.
const fees$ = of(
15000,
25000,
35000,
45000
);
const selectedFees$ =
fees$.pipe(
filter(fee =>
fee >= 30000
)
);
RxJS and Asynchronous Programming
RxJS is useful when an application needs to handle values that become available asynchronously.
HTTP requests, timers, events and other ongoing streams can be represented and processed using Observable-based patterns.
RxJS with Angular HttpClient
getCourses() {
return this.http.get<Course[]>(
'/api/courses'
);
}
this.courseService
.getCourses()
.pipe(
map(courses =>
courses.filter(
course => course.active
)
)
)
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
}
});
CIIT Training Institute Example
Suppose the CIIT Training Institute dashboard receives different course enquiries.
RxJS can be used to process the enquiry stream before the final data is displayed in the dashboard.
const enquiries$ = of(
'Java',
'.NET',
'Python',
'Data Analytics'
);
const result$ =
enquiries$.pipe(
map(course =>
course + ' enquiry'
),
filter(message =>
message.includes(
'Java'
) ||
message.includes(
'.NET'
)
)
);
result$.subscribe({
next: enquiry => {
console.log(enquiry);
}
});
CIIT Training Institute
Java enquiry
.NET enquiry
Benefits of RxJS
Reactive
Works naturally with values arriving over time.
Composable
Operators can be combined into processing pipelines.
Asynchronous
Useful for asynchronous application operations.
Stream Based
Represents and processes sequences of values.
Important RxJS APIs
| API / Concept | Purpose | Example |
|---|---|---|
| Observable | Represents a stream | new Observable() |
| subscribe() | Consumes emitted values | observable.subscribe() |
| pipe() | Connects operators | observable.pipe(...) |
| map() | Transforms values | map(value => value * 2) |
| filter() | Filters values | filter(value => value > 10) |
Important Points
RxJS provides tools for working with values that arrive over time.
Observables represent streams that can be consumed by subscribers.
Operators allow streams to be transformed, filtered, combined and controlled.
Multiple operators can be connected using pipe().
RxJS Best Practices
-
Give Observable variables meaningful names, commonly using
a naming convention such as a trailing
$. - Keep RxJS pipelines readable and avoid unnecessary complexity.
- Use suitable operators instead of manually processing every emitted value.
- Handle important asynchronous errors properly.
- Manage long-lived subscriptions carefully.
- Keep stream-processing logic separated from presentation logic where practical.
Summary
RxJS is a reactive programming library that provides Observables and operators for working with asynchronous and time-based data streams.
Important concepts include Observable, subscribe(), pipe() and operators.
In the next topic, we will study RxJS Operators in detail.