Replay Subject - RXJS

Replay Subject - RXJS

In this tutorial, we will learn about the Replay Subject of RXJS.

Replay Subject

ReplySubject stores multiple old Emittedvalue according to the specified time

For example we can store the last 3 value for the last 3 min we can do that

import { ReplaySubject} from 'rxjs';

mySubject = new ReplaySubject<string>(3, 5000);

this.mySubject .next(1);
this.mySubject .next(2);
this.mySubject .next(3);
this.mySubject .next(4);


mySubject.subscribe(res => {
  console.log('1st sub:', res);
});

setTimeout(() => {
mySubject.subscribe(res => {
  console.log('2nd sub:', res);
});

},1000)



Above will give output:

1st sub: 1
1st sub: 2
1st sub: 3
1st sub: 4
2nd sub: 3
2nd sub: 4

The Above example will store the last three emissions for 5 seconds as we are subscribing seconds subscriber after one second it will show the last two values

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

You might also like

Leave a Comment