ReplaySubject is a special variant of the Subject that emits old values to observers even though at the time of execution of those values the observer is not created. It provides for the setting of the number of old values that it should remember which will be emitted once a new subscriber registers with it.

A simple demonstration for replay subject is given with the following code snippet.

import { ReplaySubject } from 'rxjs' ;

const rs$ = new ReplaySubject( 2 ); // (1)
// subscriber A
rs$.subscribe((data) => { // (2)
console.log( 'subscriber A: ' + data);
});
rs$.next(Math.random()); // (3)
rs$.next(Math.random());
rs$.next(Math.random());
// Subscriber B
rs$.subscribe((data) => { // (4)
console.log( 'subscriber B: ' + data);
});
rs$.next(Math.random());

#javascript #training #programming #jobs #software-development

All you need to know about Replay Subject in RxJS
1.10 GEEK