RxBus - an Event Bus Based on RxJava and Optimized for Android

Event bus is a great solution that allows objects with different lifecycles and located in different layers of hierarchy to communicate.

I venture to suggest that if you have an event bus in your Android application, most likely you use libraries like Otto or EventBus.

However, Otto from Square was officially deprecated and its page on GitHub says: “This project is deprecated in favor of RxJava and RxAndroid.”

As we already use RxJava and RxAndroid in the development, we decided to try out the Rx approach to implementation of event bus pattern. Of course, we did our best to save a clean api driven by annotations and simple to use event bus library like Otto.

This is how RxBus library was made. This open source library is available on GitHub.

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();

By default, the Bus enforces that all interactions occur on the main thread. To subscribe to an event, you can declare and annotate a method with @Subscribe. The method should be public and should take only a single parameter.

@Subscribe
public void onEvent(SomeEvent event) {
  // TODO: Do something
}

You can also add a filter and an alternate enforcement in your program by creating subscription with the following:

CustomSubscriber<SomeEvent> customSubscriber = bus.obtainSubscriber(SomeEvent.class,
  new Consumer<SomeEvent>() {
    @Override
    public void accept(SomeEvent someEvent) throws Exception {
      // TODO: Do something
    }
  })
  .withFilter(new Predicate<SomeEvent>() {
    @Override
    public boolean test(SomeEvent someEvent) throws Exception {
      return "Specific message".equals(someEvent.message);
    }
  })
  .withScheduler(Schedulers.trampoline());

To receive events, a class instance would need to register with the bus:

bus.register(this);

A custom subscriber also needs to register with the bus:

bus.registerSubscriber(this, customSubscriber);

When appropriate, you would also need to call the unregister method:

bus.unregister(this);

To publish a new event, you can call the post method:

bus.post(new SomeEvent("Message"));

To add RxBus to your project (as a Gradle dependency), use:

compile 'com.github.anadea:rxbus:1.0.1'

If you have any suggestions on how to improve the RxBus implementation, don’t hesitate to contact us.

Don't want to miss anything?

Subscribe and get stories like these right into your inbox.

Keep reading

Integrating Self-Storage Services in Web Software

Integrating Self-Storage Services in Web Software

Self-storage facilities are companies that provide storage space (lockers, containers, garages, etc.) by lease. Often, self-storage companies have multiple locations with a few hundred of units in each. It is quite a big business and it requires management software to keep all units under control. There is special software for their needs.

How to Speed Up Your Tests via :build_stubbed

How to Speed Up Your Tests via :build_stubbed

Rspec is an awesome thing that was created for ruby community. Most of us write tests. However, sometimes in large projects our test becomes really slow. So, each launch of the test really hurts and it does not meter whether you launch your test before commit/push or on CI. When your test suits pass over 30 minutes  -  something definitely went wrong.

Contact us

Let's explore how our expertise can help you achieve your goals! Drop us a line, and we'll get back to you shortly.