In this article, we are going to discuss the basics of observable and subject. Also, the difference between them is explained with the help of practical examples and the different types of subjects.
Agenda
- What is Angular?
- Observable in Angular
- Subject in Angular
- Difference between Observable and Subject with a Real-Time Use Case
- Types of Subjects in Angular with Examples
Prerequisites
- Basic understanding of typescript
- VS Code
- Angular CLI
- NodeJS
What is Angular?
Angular is a popular open-source JavaScript framework for building web applications. It was developed by Google and is currently maintained by the Angular Team at Google. Angular allows developers to create dynamic, single-page applications (SPAs) and provides a structured approach to building complex web applications.
Observable in Angular
- In Angular, Observables are part of the Reactive Extensions for JavaScript (RxJS) library.
- Observables provide support for passing messages between parts of your application.
- Observables are a powerful feature used extensively in reactive programming to handle asynchronous operations and data streams.
- Observables provide a way to subscribe to and receive notifications when new data or events are emitted, enabling you to react to changes in real-time.
Subject in Angular
- An RxJS Subject is a special type of Observable that allows values to be multicast to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast.
- A Subject is like an observable but can be multicast to many Observers. Subjects are like Event Emitters: they maintain a registry of many listeners.
- It is a part of the Reactive Extensions for JavaScript (RxJS) library, which is included by default in Angular projects.
Difference between Observable and Subject with Real-Time Use Case
Observable and Subject are both part of the Reactive Extensions for JavaScript (RxJS) library and are used for managing asynchronous data streams and event handling in Angular. However, they have some key differences

Types of Subjects in Angular with Examples
Following are the types of Subjects available in the RxJS library, which you can use for different scenarios:
1. Subject
- The Subject is a basic implementation of a subject in RxJS.
- It is a multicast observable that maintains a list of observers and notifies all of them when a new value is emitted using the next() method.
- It does not have an initial value, so subscribers only receive values emitted after they subscribe.
- This is a good option when you need a simple multicast subject without any additional features.
//------------------Observables are unicast-----------------
//observable
let observable = new Observable<number>(ele =>
ele.next(Math.random()))
//first subscriber
observable.subscribe(result => {
this.first_subscriber_observable = result;
console.log(result)
})
//second subscriber
observable.subscribe(result => {
this.second_subscriber_observable = result;
console.log(result)
})
//third subscriber
observable.subscribe(result => {
this.thrid_subscriber_observable = result;
console.log(result)
})
//--------------------------------------------------------
//------------------Subjects are multicast-----------------
//subject
let subject = new Subject<number>()
//first subscriber
subject.subscribe(result => {
this.first_subscriber_subject = result;
console.log(result)
})
//second subscriber
subject.subscribe(result => {
this.second_subscriber_subject = result;
console.log(result)
})
//third subscriber
subject.subscribe(result => {
this.third_subscriber_subject = result;
console.log(result)
})
subject.next(Math.random())
//--------------------------------------------------------




Join the conversation! Your thoughts help the community grow.