By Timo Tuominen

In this article we’ll take a quick look at subscriptions in RxJava programming. To see how subscriptions work we’ll take a real-life example of a newspaper subscription.

 

Save 37% off Grokking FRP with code fcctuominen at manning.com.


Subscriptions Explained

The subscribe function returns a Subscription object that allows us to manage the created relationship between the observable and the subscriber. “Us” in this context is the creator of the logic—the observables and subscribers are building blocks at our disposal.

Imagine that the following newspaper is delivered to the mail box each morning:


Figure 1


Subscription Dynamics

After the subscription is created, it defines a relationship between the source and the target—or the observable and subscriber. This we’ve seen before:


Figure 2


But in our example, isn’t Peter the subscriber?

The answer is linguistically yes, but in Rx terms Peter only establishes the relationship between the mailbox and the newspaper company. He’s the one who defines how the two should interact.

This may sound a little strange at first. Think about it this way: the mailbox doesn’t have any control of what is delivered into it. The same way an Rx subscriber doesn’t know where the data comes from. It’s completely unaware of a subscription.

What’s Peter then? It turns out Peter is the program logic we write: the graph itself. The graph logic are the instructions that describe how the program should work.


Figure 3


In this case the logic could be written as:

 
newYorkTimesObservable.subscribe(petersMailbox);
 

 


Terminating Subscriptions

Two conditions exist for terminating a subscription:

  1. The observable signals THAT it’s completedLet’s say the newspaper subscription Peter purchased was for 12-months and he didn’t renew it. After twelve months, the New York Times (the observable) one-sidedly considers the subscription completed and stops sending papers.The mechanism in our Rx world would be akin to a letter of termination. The newspapers stop coming and, as a standard procedure, New York Times sends a letter telling the subscriber that they shouldn’t expect any more. (At this point in RxJava the subscription is automatically released.)

    Figure 4

    Our network observables work in this way: after emitting the data retrieved from the network they send the onComplete signal and cut off the subscription.

  2. The subcription object is used to cancel the subscription
    The second scenario is less common. Usually when we need to forcefully terminate a subscription it’s because something happened that made it irrelevant or undesirable.In our imaginary scenario we can say Peter has moved to another country and doesn’t wish to receive the newspapers anymore. He calls the company, gives his subscription information, and asks them not to send the paper anymore. The subscription is terminated on the initiation of Peter.In this scenario the observable never completes and we wouldn’t receive the termination letter (the onComplete notification).

    Figure 5


In terms of code, the whole scenario could look like this:

 
Subscription subscription =

     newYorkTimesObservable.subscribe(petersMailbox);




// Time passes..

// Unsubscribe in case the subscription is still active

if (!subscription.isUnsubscribed()) {

    subscription.unsubscribe();

}
 

This is how all subscriptions work in RxJava. In other Reactive libraries there are different ways—some don’t even have subscriptions—but for this article let’s assume the graphs are always established with subscriptions.

That’s all for this article. For more, download the free first chapter of Grokking FRP.