📢
RxJS & Observables

Subjects

Observable + Observer, Dono Ek Saath
💡 Subject ek WhatsApp group jaisa hai — koi bhi MESSAGE (value) bhej sakta hai (Observer), aur SAB group members receive karte hain (Observable) — normal Observable ek "broadcast-only radio station" hai, Subject dono directions handle karta hai.

Subject EK SAATH Observable (subscribe kiya ja sakta hai) AUR Observer (values "push" ki ja sakti hain, .next() se) hai — components ke BEECH communicate karne ke liye common (jaise EventBus pattern). BehaviorSubject ek SPECIAL variant hai — HAMESHA current value rakhता hai, LATE subscribers ko turant last value milती hai.

import { Subject, BehaviorSubject } from 'rxjs';

// Regular Subject — koi initial value nahi:
const messageSubject = new Subject<string>();
messageSubject.subscribe(msg => console.log('Received: ' + msg));
messageSubject.next('Hello!');   // "Received: Hello!"

// BehaviorSubject — INITIAL value zaroori, LATEST value hamesha available:
const userSubject = new BehaviorSubject<string>('Guest');
userSubject.subscribe(user => console.log('Current user: ' + user));
// TURANT "Current user: Guest" print hota hai (initial value)

userSubject.next('Aarav');   // "Current user: Aarav"

// Service mein — components ke beech state share karna:
@Injectable({ providedIn: 'root' })
export class NotificationService {
  private notificationSubject = new Subject<string>();
  notifications$ = this.notificationSubject.asObservable();

  notify(message: string) {
    this.notificationSubject.next(message);
  }
}
📢
Subject ek WhatsApp group jaisa hai — koi bhi MESSAGE (value) bhej sakta hai (Observer), aur SAB group members receive karte hain (Observable) — normal Observable ek "broadcast-only radio station" hai, Subject dono directions handle karta hai.
1 / 6
⚡ Quick Recap
  • Subject = Observable + Observer, dono ek saath
  • BehaviorSubject = current value HAMESHA available, late subscribers ko bhi milती hai
  • .asObservable() = write access chhupaओ, sirf read expose karo
On this page (2 subtopics)

ReplaySubject(n) LAST n values YAAD rakhta hai, aur NAYE subscribers ko turant wo values deता hai (BehaviorSubject sirf LAST EK value yaad rakhta hai, ReplaySubject MULTIPLE).

import { ReplaySubject } from 'rxjs';

const recentActions$ = new ReplaySubject<string>(3);   // last 3 values

recentActions$.next('Action 1');
recentActions$.next('Action 2');
recentActions$.next('Action 3');

recentActions$.subscribe(action => console.log(action));
// TURANT teeno values print hoti hain: Action 1, Action 2, Action 3
// (chahe subscribe LATE hui ho)
💡Tip: ReplaySubject rare use-case hai — mostly "activity log" jaisi features ke liye jaha naye subscribers ko RECENT history dekhनी chahiye, sirf CURRENT state nahi (jo BehaviorSubject deता hai).

AsyncSubject SIRF LAST value emit karta hai, AUR SIRF jab .complete() call ho — beech ke SAARE values IGNORE ho jaate hain. Rare, specific use-case hai (jaise "sirf final result chahiye, intermediate progress nahi").

import { AsyncSubject } from 'rxjs';

const result$ = new AsyncSubject<number>();
result$.subscribe(val => console.log('Final: ' + val));

result$.next(1);   // kuch nahi hota
result$.next(2);   // kuch nahi hota
result$.next(3);
result$.complete();   // AB "Final: 3" print hota hai
💡Tip: AsyncSubject rarely directly use hota hai — Promise-jaisa behavior chahiye ho (sirf FINAL result) to usually toPromise()/firstValueFrom() ya simple Promise use karna zyada common/readable hota hai.