Angular Subscription
Learn how to subscribe to an Observable, receive emitted values, handle errors and completion, and work with active subscriptions in Angular applications.
What is a Subscription?
A Subscription represents an active execution of an Observable.
When we call subscribe(), the application starts listening to values emitted by the Observable.
The returned Subscription object can also be used to stop the Observable execution when required.
Subscription Flow
Basic subscribe() Syntax
observable.subscribe({
next: value => {
console.log(value);
},
error: error => {
console.error(error);
},
complete: () => {
console.log('Completed');
}
});
Observer Callbacks
next
Runs whenever the Observable emits a value.
error
Runs when the Observable reports an error.
complete
Runs when the Observable completes successfully.
Subscription
Represents the active execution created by subscribe().
Simple Subscription Example
Consider an Observable that emits three course categories.
const courses$ =
new Observable<string>(subscriber => {
subscriber.next('Java Full Stack');
subscriber.next('.NET Full Stack');
subscriber.next('Python Full Stack');
subscriber.complete();
});
courses$.subscribe({
next: course => {
console.log(course);
},
complete: () => {
console.log('Course stream completed');
}
});
Console Output
Java Full Stack
.NET Full Stack
Python Full Stack
Course stream completed
Storing the Subscription
The subscribe() method returns a Subscription object.
We can store that object in a variable when the application needs to manage the subscription later.
const courseSubscription =
courses$.subscribe({
next: course => {
console.log(course);
}
});
Subscription Object
Active
Represents whether the subscription is still active.
unsubscribe()
Stops the subscription when it is no longer required.
Subscription
Holds the execution created by subscribe().
Multiple
An application can have multiple active subscriptions.
Handling Errors in Subscription
The error callback is used when an Observable reports an error.
Error handling helps the application respond gracefully instead of ignoring failures.
courseService.getCourses()
.subscribe({
next: courses => {
console.log(courses);
},
error: error => {
console.error(
'Unable to load courses',
error
);
}
});
Handling Completion
The complete callback executes when the Observable finishes successfully.
It can be used for actions that should happen after the complete data stream has finished.
courses$.subscribe({
next: course => {
console.log(course);
},
complete: () => {
console.log(
'All courses received'
);
}
});
Subscription with HttpClient
Angular HttpClient methods commonly return Observables.
The application can subscribe to the returned Observable to receive the HTTP response.
getCourses() {
return this.http.get<Course[]>(
'/api/courses'
);
}
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
}
});
CIIT Training Institute Example
Suppose the CIIT Training Institute dashboard receives course enquiry notifications.
The dashboard can subscribe to an Observable and process each new enquiry whenever it is emitted.
const enquirySubscription =
enquiryService.getEnquiries()
.subscribe({
next: enquiry => {
console.log(
'New enquiry:',
enquiry
);
},
error: error => {
console.error(error);
}
});
CIIT Enquiry Dashboard
New enquiry received
Course: Java Full Stack
Course: .NET Full Stack
Subscription is receiving updates...
Multiple Subscriptions
A component may subscribe to more than one Observable when different data sources are required.
const coursesSubscription =
courses$.subscribe({
next: course => {
console.log(course);
}
});
const enquiriesSubscription =
enquiry$.subscribe({
next: enquiry => {
console.log(enquiry);
}
});
When multiple subscriptions are created, they should be managed carefully according to how long they need to remain active.
Subscription Lifecycle
Observable vs Subscription
| Feature | Observable | Subscription |
|---|---|---|
| Purpose | Represents a data stream | Represents an active execution |
| Created By | Observable source | subscribe() |
| Receives Values | Emits values | Receives emitted values |
| Management | Defines the stream | Can be stopped using unsubscribe() |
| Multiple Consumers | Possible | Each subscribe() creates an execution |
Subscription Best Practices
- Subscribe only when the application actually needs the Observable values.
- Handle errors where appropriate instead of silently ignoring failures.
- Keep subscription logic easy to understand and maintain.
- Manage long-lived subscriptions carefully.
- Avoid keeping subscriptions active after they are no longer required.
- Use suitable Angular or RxJS patterns for managing asynchronous streams.
Important Points
1. Calling subscribe() starts listening to an Observable.
2. The returned object is a Subscription.
3. next handles emitted values.
4. error handles Observable errors.
5. complete runs when the Observable completes successfully.
Summary
A Subscription represents an active execution of an Observable.
The subscribe() method is used to receive values through callbacks such as next, error and complete.
Subscriptions should be managed carefully, especially for long-lived streams. In the next topic, we will learn about Data Streams.