52

I'm trying to get my head around the golden rule (if any) about:

When to use BehaviorSubject ?

and

When to use PublishSubject ?

The difference between them is very clear

There are many kinds of subjects. For this specific requirement, a PublishSubject works well because we wish to continue the sequence from where it left off. So assuming events 1,2,3 were emitted in (B), after (A) connects back we only want to see 4, 5, 6. If we used a ReplaySubject we would see [1, 2, 3], 4, 5, 6; or if we used a BehaviorSubject we would see 3, 4, 5, 6 etc. (source : How to think about Subjects in RxJava (Part 1))

I have seen that Subject's are used in two contexts (at least), UI context and listener context.

  • UI context (MVVM as example)

For example here a BehaviorSubject is used, and it's clear why they use Subject and not Observable but I have changed the BehaviorSubject to PublishSubject but the app behavior still the same.

  • Listener context

Why they make project field a BehaviorSubject and not PublishSubject ?

A O
  • 5,516
  • 3
  • 33
  • 68
TooCool
  • 10,598
  • 15
  • 60
  • 85
  • A little sample that show the difference between them : https://github.com/selmanon/BehaviorAndPublishSubjectDemo – TooCool Jul 21 '19 at 11:42

4 Answers4

44

The main difference between PublishSubject and BehaviorSubject is that the latter one remembers the last emitted item. Because of that BehaviorSubject is really useful when you want to emit states

Why they make project field a BehaviorSubject and not PublishSubject ?

Probably because they want to be able to retrieve the last emitted project with this method:

@Override public @NonNull Observable<Project> project() {
  return this.project;
}
user5480949
  • 1,410
  • 1
  • 15
  • 22
Dmitry
  • 2,326
  • 1
  • 13
  • 18
  • 1
    Does it only emit state changes or even when state changes to the same previous value? Like for example if I put onNext('Adam'), onNext('Adam'), onNext('Smith') Would it emit only Adam followed by Smith? – Saifur Rahman Mohsin Sep 07 '19 at 10:30
  • 1
    @SaifurRahmanMohsin See https://github.com/akarnokd/RxJava2_9/blob/master/src/main/java/io/reactivex/subjects/BehaviorSubject.java#L267. There's no check to see if the current value has changed at all. – Navneeth Feb 21 '20 at 03:46
39

PublishSubject: Starts empty and only emits new elements to subscribers. There is a possibility that one or more items may be lost between the time the Subject is created and the observer subscribes to it because PublishSubject starts emitting elements immediately upon creation.

BehaviorSubject: It needs an initial value and replays it or the latest element to new subscribers. As BehaviorSubject always emits the latest element, you can’t create one without giving a default initial value. BehaviorSubject is helpful for depicting "values over time". For example, an event stream of birthdays is a Subject, but the stream of a person's age would be a BehaviorSubject.

H S W
  • 6,310
  • 4
  • 25
  • 36
21

Publish Subject: Here, if a student entered late into the classroom, he just wants to listen from that point of time when he entered the classroom. So, Publish will be the best for this use-case.

Behavior Subject: Here, if a student entered late into the classroom, he wants to listen the most recent things(not from the beginning) being taught by the professor so that he gets the idea of the context. So, here we will use Behavior.

Pradeep Bishnoi
  • 1,843
  • 2
  • 22
  • 26
  • 2
    Love this description as an understandable alternative to the classic technical explanation. Thank you! – kjanderson2 Dec 02 '20 at 17:12
  • Publish Subject: "listen from that point of time when he entered". Behavior Subject: "listen to the most recent things". Appreciate the way you wrote it but I still find it confusing. They both mean the same thing to me. Please clarify – Anmol Singh Feb 01 '23 at 11:52
  • 1
    @AnmolSingh, Suppose we enter the math class and the teacher teaches addition, subtraction, multiplication and division. The teacher has explained addition and subtraction, and multiplication division is left. If you enter the class now and are publish subject then you will learn about multiplication and division. If you are Behavior Subject then you would know about Subtraction as well because that was the last topic teacher taught before you entered the class, in total, you would learn subtraction multiplication and division if you are behaviour Subject. – Shubham Jain May 30 '23 at 17:06
2

The difference on BehaviourSubject and PublishSubject relies on how long they keep the data they captures, in instance the PublishSubject only keeps the data available at moment and keeps updating on every entry while BehaviourSubject keeps the last data inserted, so you may use for example to confirm password on a signup form and as an example for PublishSubject, performing a search and it has to update the data constantly in order to give accurate results and there's no too much necessity to compare data that are being inserted.

As reference i leave this two photos from http://reactivex.io/documentation/subject.html

PublishSubject

BehaviourSubject

Igor L Sambo
  • 121
  • 7