Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    ReplaySubject: remembering events from the past


    A BehaviorSubject can remember the latest value emitted, but what if we wanted Observer B to see all the previous values emitted in the past? We can't do that with BehaviorSubject, but there is ReplaySubject, which allows us to do that. This lessons teaches you everything you need to know about ReplaySubjects.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    A behavior subject can remember the latest value emitted, but if we wanted observer B to see all the previous values emitted in the past, or what if we wanted observer B to see the latest two values? With behavior subject, we just can't do that, but we can do it with a replay subject.

    The constructor doesn't take anymore this initial value. We actually won't see this zero anymore with the replay subject. Here, the first argument would be a buffer size. Here, just for first example, we're going to use a buffer size of two, which means that when B subscribes, it will see the latest two values.

    That's the size of the buffer that exists inside the replay subject. When we run this, when B subscribes, it's going to see the latest two values. We can even use a larger argument here, like positive infinity, for instance.

    This will simply remember all of the events that happened in the past, because the buffer size now is unbounded. It's going to remember all of the events that happened in the past. When B subscribes it, we'll just emit all of those events from the past.

    That's why it's even called a replay subject, because it essentially replays everything that happened once a new subscriber arrives. Besides that, replay subject can take a second argument here, called window size, for the constructor.

    Window size determines how long in time will the replay subject keep events stored in its internal buffer. If I put in here 1,000 milliseconds, those events older than one second will not be any more replayed to a late subscriber.

    In our example here, we have synchronous admissions, but those are not interesting when we're talking about time, so let's put these in a set timeout, for instance, to run after a couple of milliseconds. Let's say after 100 milliseconds, we're going to run that.

    After 200 milliseconds, we're going to emit two. After 300 milliseconds, we're going to emit three. Let's say that observer B would subscribe at 400 milliseconds. It would look something like this in time. B was subscribed at some point here.

    If we have a window size of 250 milliseconds, then B will see those events that happened at almost 250 milliseconds in the past. It would see two and three, like this. If we run this, we're going to see B will see only two and three.

    Apparently, with a replay subject, we can replay more events from the past than we can with a behavior subject, because we can customize it with a buffer size parameter, and window size parameter. Does that mean that a replay subject of buffer size one is the same thing as a behavior subject?

    That's not entirely true. As we see here, even though B is subscribed late, and it sees the latest value, three, there are some differences. If we did it with a behavior subject, we would have to provide here the seed argument or the initial value for the behavior subject.

    We had that as zero, so it means that the behavior subject always has a value, and initially, that value is zero. That's not true with a replay subject, because initially, it doesn't have any value, or no event happened.

    In fact, if you would subscribe here in the beginning, as we did with observer A, A did not see any event previously. It only saw the next events, which were one, two, and three. That's one fundamental difference between a replay subject and a behavior subject.

    It's because replay subjects don't actually represent values over time, like your age, that we saw. It just really replays events from the past. Another difference is with how these subjects handle complete.

    Here, if we complete the subject after, let's say, 350 milliseconds, and also in the diagram here, would complete somewhere there, then what we subscribe with observer B, after 400 milliseconds, then the replay subject will replay the latest value, and also complete.

    Now if we had a buffer size of 100, for instance, then it would replay all the previous, at most 100 events from the past, so it would replay one, two, and three. If we run this, we see after the subject completes, B subscribes, and B sees the events, one, two, three.

    It means that the replay subject is replaying events for late observers, even after the subject completed. It basically replays no matter if it has completed or not. That's not the case for a behavior subject. Remember, a behavior subject represents a value over time.

    It always has a value. Now once we complete, it means that that value has completed. If we try to observe after a complete happens with observer B, then there's nothing to be observed. There's no value to be observed.

    Here, we're just going to see the complete. Once we do that with a behavior subject, it's just going to see done. It's not going to see, actually three and done. The behavior subject usually replays the last one value.

    The main difference is that, if you want to represent the value over time, then you're looking for a behavior subject. If you just want to literally replay events from the past, then you're looking for a replay subject.