Behavior and Replay Subjects
We explore two types of Subjects that buffer emitted values for Observers that subscribe at a later point in time.
We'll cover the following
Behavior Subject
BehaviorSubject
is a special type of Subject that buffers the latest emitted value and pushes that value to every new Observer that subscribes to it. As simple as that.
The following code illustrates a simple implementation of BehaviorSubject
, which must be initialized with a default value in order to fulfill its purpose.
In a group chat room, every new user can be greeted with a default Welcome message. After some chit chat between the initial users, someone who is late to the party always decides to join. However, the chat allows only one previously sent message to be seen by the newly joined users. How do we implement his scenario using RxJS?
The following code demonstrates how BehaviorSubject
can save the day.
Get hands-on with 1200+ tech skills courses.