RxJava Subject With Examples

In this tutorial, we’ll be discussing Subjects in RxJava. We won’t be covering the basics of RxJava. If you are new to RxJava, do read this tutorial before proceeding ahead.

What is a Subject?

We know Observables and Observers. Observables emit data. Observers listen to those emissions by subscribing with the help of Subscribers.

Subjects can emit and subscribe to the data. They have the implementations of Observables as well as Observers.

A Subject has the same operators that an Observable has.
They can multicast too. This means all the Observers subscribed to it will receive the same emissions from the point of subscription.
This prevents doing duplicate operations for multiple subscribers.

Where is a Subject used?

Subjects are commonly used when you need to observe some data and then later can emit that data to other observers.

Let’s look at an example with and without Subject. We’ll see how Subject saves us from redundant operations.

We’ve subscribed two Observers. Let’s see what the output prints.

rxjava-observable-observer-output

The map operation is computed each time for each observer.

By using Subject we can remove this redundancy as shown below:

PublishSubject is a subclass of Subject.

The output is:

rxjava-observable-observer-output

So the map operation isn’t repeated for the second subscriber.
Thus the Subject multicasts to all of its subscribers. That means that the data would be emitted and computed just once.

Cold Observables vs Hot ObservablesLet’s explain this with a metaphor.

Case 1
You are watching a movie on Youtube. You call your friend and tell him to subscribe to that channel and watch it too. He opens his Youtube and starts the movie.

Case 2
You are watching a live cricket/football match. Your friend joins you after some time.

In Case 1, you and your friend would be able to watch the full movie from the beginning.
In Case 2, the friend won’t be able to watch the match from the beginning.

  • Case 1 = Cold Observables. All observers would get the emissions from the beginning.
  • Case 2 = Hot Observables. Every Observer would receive emissions only from the point when they subscribed.

Subjects are Hot Observables

Now that we’re clear about Hot Observables, let’s validate whether Subjects are Hot Observables or not with an example:

The output printed in the console is:

rxjava-observable-observer-output

As you can see the Observer 2 and Observer 3 don’t get the values emitted by the Subject before the point of subscription.

Another metaphor for Hot Observables – They are like Whatsapp Group messages. You can only read messages after joining the group

Types of Subject

Following are the different types of Subjects. Each works differently.

  • PublishSubject
  • BehaviorSubject
  • AsyncSubject
  • ReplaySubject
  • UnicastSubject
  • SingleSubject

PublishSubject

This emits all the items at the point of subscription. This is the most basic form of Subject and we’ve implemented it above.

BehaviorSubject

An observer, when subscribed to the BehaviorSubject, would get the last emitted item before it subscribed and all subsequent items.

Metaphor: Your friend gets to watch the last replay when he joins for the cricket match besides viewing the rest of the live match.

Example:

Output:

rxjava-behavior-subjects

AsyncSubject

Of all the emissions, AsyncSubject would emit only the last item. That too only when onComplete() gets called.

Metaphor: You turned on your television to watch the live cricket match and its the last ball of the game. If your friend joins you late, he/she can also just see the last ball of the game.

Example:

Output:

rxjava-behavior-subjects

As you can see if the onComplete was called before the second observer subscribed, still that observer would get the last emitted value, even if it was before it subscribed.

ReplaySubject

As the name says, when an observer subscribes to a ReplaySubject, it would get all the items from the beginning.

Metaphor: Your friend who had missed the live cricket match can now replay the whole. He probably recorded it!

Example:

Output:

rxjava-behavior-subjects

UnicastSubject

A UnicastSubject can have a single observer only. Otherwise it will throw an error. Also it emits all the values from the beginning.

Metaphor: You and only you are watching the live cricket match. You can rewind it. If your friend comes your television screen won’t be visible to him. It will still be to you.

Example:

Output:

rxjava-unicastsubject

SingleSubject

Last but not the least, a SingleSubject can emit only one item to all its observers. Multiple observers can subscribe to it. It can subscribe to multiple SingleObservables.

Metaphor: You can only watch the last ball of the match. Your friend can also just watch the single last ball of the match.

Example:

Output:

rxjava-unicastsubject

PS: We can set transformation operators on the Subject in the same way as we do on Observables:

Only 2 would be printed.

And that sums up RxJava Subject for us.

By admin

Leave a Reply

%d bloggers like this: