Communicating Among Modules

1. Overview - How to communicate among different app modules?

To get the context of the section, please refer this section.

2. Usage tips

We recommend obtaining a single instance of bus through injection or another appropriate mechanism. Alternatively, you may get a singleton like the following:

Bus bus = BusProvider.getInstance();

Or You could provide a getter at your application level class to get the instance of the RxBus. (like we have used).

By default, the Bus enforces that all interactions occur on the main thread.

You can create RxBus like below.

public class RxBus {
public RxBus() {
private PublishSubject<Object> bus = PublishSubject.create();
public void send(Object o) {
public Observable<Object> toObservable() {
return bus;

Now, we will create the Singleton(single instance) of RxBus in our application class or at any other place as below:

How to access the RxBus has been mentioned above already.

You can subscribe for an event in any class like below:

if(exchangeObject instanceof ExchangeObject){
ExchangeObject.SignalExchangeObject signalExchangeObject=(ExchangeObject.SignalExchangeObject)exchangeObject;
}else if(exchangeObject instanceof ExchangeObject.EventExchangeObject){
ExchangeObject.EventExchangeObject eventExchangeObject=(ExchangeObject.EventExchangeObject)exchangeObject;
Timber.d("Event Received %s ",eventExchangeObject.customEvents);
Timber.d("Event Received %s ",eventExchangeObject.customEvents);
}else if(exchangeObject instanceof ExchangeObject.NotificationExchangeObject){
PendingIntent pendingIntent=((ExchangeObject.NotificationExchangeObject)exchangeObject).data.getIntent();
int notificationID=((ExchangeObject.NotificationExchangeObject)exchangeObject).data.getNotificationID();
int title=((ExchangeObject.NotificationExchangeObject)exchangeObject).data.getTitle();
String body=((ExchangeObject.NotificationExchangeObject)exchangeObject).data.getBody();
Timber.d("Event Received for Push Notification %s ",title);
Timber.d("Received but not intended");

You can send an event from any other class or from the same class like below:

MyApplication.getInstance().component().rxBus().send(new EventTypeExhangeObject());

This way, you have the EventBus pattern with RxJava, RxBus. But if there is some error, it will terminate, so to avoid that check RxRelay.

RxRelay: A Subject except without the ability to call onComplete or onError.