A Subject is a special type of Observable which shares a single execution path among observers.
You can think of this as a single speaker talking at a microphone in a room full of people. Their message (the subject) is being delivered to many (multicast) people (the observers) at once. This is the basis of multicasting. Typical observables would be comparable to a 1 on 1 conversation.
There are 4 variants of subjects:
Subject - No initial value or replay behavior.
AsyncSubject - Emits latest value to observers upon completion.
BehaviorSubject - Requires an initial value and emits its current value (last emitted item) to new subscribers.
ReplaySubject - Emits specified number of last emitted values (a replay) to new subscribers.
​AsyncSubject​
​BehaviorSubject​
​ReplaySubject​
​Subject​
( Stackblitz )
/*s1 n(r) n(x) s2 n(j) c n(s)Subjects1 ^-----r------x--------------j------|----------s2 ---------------------^------j------|----------AsyncSubjects1 ^----------------------------------j|---------s2 ---------------------^-------------j|---------BehaviorSubjects1 ^a----r------x--------------j------|----------s2 ---------------------^x-----j------|----------ReplaySubjects1 ^-----r------x--------------j------|----------s2 ---------------------^r-x---j------|----------*/​// RxJS v6+import { Subject, AsyncSubject, BehaviorSubject, ReplaySubject } from 'rxjs';​const subject = new Subject();const asyncSubject = new AsyncSubject();const behaviorSubject = new BehaviorSubject('a');const replaySubject = new ReplaySubject(2);​const subjects = [subject, asyncSubject, behaviorSubject, replaySubject];const log = subjectType => e => console.log(`${subjectType}: ${e}`);​console.log('SUBSCRIBE 1');subject.subscribe(log('s1 subject'));asyncSubject.subscribe(log('s1 asyncSubject'));behaviorSubject.subscribe(log('s1 behaviorSubject'));replaySubject.subscribe(log('s1 replaySubject'));​console.log('\nNEXT(r)');subjects.forEach(o => o.next('r'));​console.log('\nNEXT(x)');subjects.forEach(o => o.next('x'));​console.log('\nSUBSCRIBE 2');subject.subscribe(log('s2 subject'));asyncSubject.subscribe(log('s2 asyncSubject'));behaviorSubject.subscribe(log('s2 behaviorSubject'));replaySubject.subscribe(log('s2 replaySubject'));​console.log('\nNEXT(j)');subjects.forEach(o => o.next('j'));​console.log('\nCOMPLETE');subjects.forEach(o => o.complete());​console.log('\nNEXT(s)');subjects.forEach(o => o.next('s'));​/*OUTPUT:​SUBSCRIBE 1s1 behaviorSubject: a​NEXT(r)s1 subject: rs1 behaviorSubject: rs1 replaySubject: r​NEXT(x)s1 subject: xs1 behaviorSubject: xs1 replaySubject: x​SUBSCRIBE 2s2 behaviorSubject: xs2 replaySubject: rs2 replaySubject: x​NEXT(j)s1 subject: js2 subject: js1 behaviorSubject: js2 behaviorSubject: js1 replaySubject: js2 replaySubject: j​COMPLETEs1 asyncSubject: js2 asyncSubject: j​NEXT(s)*/
​Official Overview​
📰
​Official Documentation​
📰
​On The Subject Of Subjects (in RxJS)​
📰 - Ben Lesh