RxJS Combining Observables
Learn how RxJS can combine multiple Observable streams and produce useful results from different data sources.
What is Combining Observables?
In real applications, data often comes from more than one Observable source.
RxJS provides combination operators that allow multiple Observables to work together and produce a new Observable.
Common combination operators include combineLatest(), forkJoin(), merge() and concat().
Combining Observable Flow
Why Combine Observables?
Multiple Data Sources
Work with data coming from different Observables.
Coordinated Data
Produce results based on multiple streams.
API Coordination
Coordinate multiple asynchronous API operations.
Unified Result
Create one stream from multiple related sources.
combineLatest()
combineLatest() combines the latest values emitted by multiple Observables.
After each source has emitted at least one value, a new combined value can be emitted whenever any source emits again.
const course$ = of(
'Java',
'.NET'
);
const mode$ = of(
'Online',
'Offline'
);
const result$ =
combineLatest([
course$,
mode$
]);
result$.subscribe({
next: result => {
console.log(result);
}
});
Combined Result
Course: Java
Mode: Online
Result is represented as a combined value.
combineLatest() Flow
forkJoin()
forkJoin() is useful when multiple Observables need to complete and the application wants their final emitted values together.
It is commonly useful for coordinating multiple finite asynchronous operations such as independent API requests.
const courses$ =
this.courseService.getCourses();
const trainers$ =
this.trainerService.getTrainers();
forkJoin({
courses: courses$,
trainers: trainers$
}).subscribe({
next: result => {
console.log(
result.courses
);
console.log(
result.trainers
);
},
error: error => {
console.error(error);
}
});
forkJoin() Flow
merge()
merge() combines multiple Observables into one Observable by forwarding their emissions into the resulting stream.
Values are emitted as the source Observables produce them, so the resulting order depends on the timing of emissions.
const java$ = of(
'Java'
);
const dotnet$ = of(
'.NET'
);
const merged$ =
merge(
java$,
dotnet$
);
merged$.subscribe({
next: course => {
console.log(course);
}
});
Output
Java
.NET
concat()
concat() subscribes to Observables in sequence.
The next Observable starts after the previous Observable completes.
const first$ = of(
'Introduction',
'Components'
);
const second$ = of(
'Services',
'Routing'
);
const topics$ =
concat(
first$,
second$
);
topics$.subscribe({
next: topic => {
console.log(topic);
}
});
Output Order
Introduction
Components
Services
Routing
Combining Operators Comparison
| Operator | Main Purpose | Behavior |
|---|---|---|
combineLatest()
|
Combine latest values | Emits when source values change after all sources have emitted |
forkJoin()
|
Combine final values | Emits when all source Observables complete |
merge()
|
Merge emissions | Forwards values as they are emitted |
concat()
|
Sequential combination | Subscribes to the next source after the previous one completes |
When to Use combineLatest()
Form Values
Combine the latest values from multiple form-related streams.
Search Filters
Combine values from different filter controls.
Dashboard Data
Work with the latest values from related streams.
Reactive UI
React to changes in multiple Observable sources.
When to Use forkJoin()
Multiple APIs
Wait for several finite API operations to complete.
Reports
Collect final results from several independent requests.
Initial Data
Load multiple independent datasets before continuing.
Final Results
Use when only the final values are required.
CIIT Training Institute Example
Suppose the CIIT Training Institute dashboard needs three independent pieces of information: courses, trainers and enquiries.
The dashboard can coordinate these Observables and prepare a combined result for display.
const courses$ =
courseService.getCourses();
const trainers$ =
trainerService.getTrainers();
const enquiries$ =
enquiryService.getEnquiries();
forkJoin({
courses: courses$,
trainers: trainers$,
enquiries: enquiries$
}).subscribe({
next: dashboard => {
console.log(
dashboard
);
},
error: error => {
console.error(
error
);
}
});
CIIT Training Institute Dashboard
Courses: Loaded
Trainers: Loaded
Enquiries: Loaded
Dashboard data is ready after the required streams complete.
Choosing the Right Combination Strategy
Combining and Transforming
Combination operators can also be followed by other RxJS operators.
This allows the application to combine streams first and then transform or filter the combined result.
const course$ = of(
'Java'
);
const mode$ = of(
'Online'
);
const result$ =
combineLatest([
course$,
mode$
]).pipe(
map(([course, mode]) => {
return course +
' - ' +
mode;
})
);
result$.subscribe({
next: result => {
console.log(result);
}
});
Output
Java - Online
Important Points
Combination operators produce an Observable based on multiple source Observables.
combineLatest(), forkJoin(), merge() and concat() have different timing and completion behavior.
Some combination strategies depend on source Observables completing before producing their final result.
A combined stream can be transformed or filtered using additional RxJS operators.
Combining Observable Best Practices
- Choose the combination operator according to the required emission and completion behavior.
- Use combineLatest() when the latest values from multiple sources are required.
- Use forkJoin() when final values from finite sources are required after completion.
- Use merge() when source emissions should flow into one stream.
- Use concat() when sources should be processed sequentially.
- Keep combined stream logic readable and avoid unnecessary combinations.
Summary
RxJS provides several operators for combining multiple Observable streams.
combineLatest() works with the latest values, forkJoin() works with final values after completion, merge() combines emissions, and concat() processes sources sequentially.
In the next topic, we will learn about Higher Order Mapping.