Angular Data Streams
Learn how data can flow through an Observable over time and how Angular applications can work with synchronous and asynchronous data streams.
What is a Data Stream?
A data stream is a sequence of values that can become available one after another.
An Observable can represent that stream and emit values whenever they become available.
The values may represent numbers, strings, objects, HTTP responses, events or other application data.
Data Stream Flow
Types of Data Streams
Synchronous
Values can be emitted immediately during execution.
Asynchronous
Values can arrive later as asynchronous work completes.
Multiple Values
A stream can emit multiple values over time.
Time Based
Some streams emit values according to time intervals.
Simple Data Stream
An Observable can emit a sequence of values one after another.
const numbers$ =
new Observable<number>(subscriber => {
subscriber.next(10);
subscriber.next(20);
subscriber.next(30);
subscriber.complete();
});
Stream Values
10
20
30
Creating a Stream with of()
The of() creation function can be used to create an Observable that emits the supplied values.
It is useful when we already have a small set of values that we want to represent as an Observable stream.
const courses$ = of(
'Java Full Stack',
'.NET Full Stack',
'Python Full Stack'
);
courses$.subscribe({
next: course => {
console.log(course);
}
});
Creating a Stream with from()
The from() creation function can create an Observable from supported iterable or promise-like sources.
For example, an array can be converted into an Observable stream.
const technologies = [
'Angular',
'TypeScript',
'RxJS'
];
const technology$ =
from(technologies);
technology$.subscribe({
next: technology => {
console.log(technology);
}
});
Output
Angular
TypeScript
RxJS
Time-Based Data Stream
The interval() creation function produces sequential numbers at a specified time interval.
This is an example of a time-based Observable stream.
const counter$ =
interval(1000);
counter$.subscribe({
next: value => {
console.log(value);
}
});
Example Output
0
1
2
3 ...
Data Stream Over Time
Each emitted value becomes available to the subscriber through the next callback.
HTTP Response as a Data Stream
Angular HttpClient commonly exposes HTTP operations as Observables.
The application can subscribe to the Observable and receive the response when it becomes available.
getCourses() {
return this.http.get<Course[]>(
'/api/courses'
);
}
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
}
});
Event Data Stream
User interactions can also be represented as streams of events.
For example, repeated button clicks or input changes can produce a sequence of event values.
User Action
↓
Event Source
↓
Observable Stream
↓
Subscriber
↓
Application Logic
CIIT Training Institute Example
Suppose the CIIT Training Institute dashboard receives course enquiry updates throughout the day.
Every enquiry can become a value in a data stream. The dashboard can subscribe to that stream and process each enquiry.
const enquiries$ =
new Observable<string>(subscriber => {
subscriber.next(
'Java Full Stack enquiry'
);
subscriber.next(
'.NET Full Stack enquiry'
);
subscriber.next(
'Python Full Stack enquiry'
);
subscriber.complete();
});
enquiries$.subscribe({
next: enquiry => {
console.log(
'New enquiry:',
enquiry
);
},
complete: () => {
console.log(
'Enquiry stream completed'
);
}
});
CIIT Enquiry Dashboard
Java Full Stack enquiry
.NET Full Stack enquiry
Python Full Stack enquiry
Common Data Stream Sources
HTTP
API responses received asynchronously.
Events
User actions and browser events.
Timer
Values generated according to time.
Data Source
Application data represented as an Observable.
Data Stream Examples
| Source | Example | Stream Behavior |
|---|---|---|
| Array | from(technologies) | Emits array values |
| Direct Values | of(10, 20, 30) | Emits supplied values |
| Timer | interval(1000) | Emits values periodically |
| HTTP | http.get() | Produces HTTP response data |
| Events | User interaction | Values can arrive as events occur |
Data Stream States
Next
A value is emitted by the stream.
Error
The stream reports an error.
Complete
The stream finishes successfully.
Unsubscribe
The consumer stops its subscription.
Why Use Data Streams?
- They represent values that can arrive over time.
- They are useful for asynchronous application operations.
- They provide a consistent way to consume different sources of data.
- RxJS operators can transform and combine streams.
- They are useful for HTTP, events, timers and other asynchronous scenarios.
Important Points
A data stream represents values that can be emitted one after another.
An Observable provides a way to represent and consume the data stream.
Some streams emit values immediately while others produce values asynchronously.
RxJS operators can be applied when data needs to be transformed, filtered or combined.
Data Stream Best Practices
- Understand where the stream data is coming from.
- Use meaningful names for Observable variables.
- Handle errors for important asynchronous operations.
- Manage long-lived streams carefully.
- Use RxJS operators when the data needs transformation.
- Avoid unnecessary active subscriptions.
Summary
A data stream represents a sequence of values that can be emitted over time.
Angular applications can work with streams produced by HTTP calls, events, timers and other data sources.
Observables provide a way to represent and consume these streams. In the next topic, we will learn about Observable Operators.