EventBus vs RxJava for Android

I am new to RxJava and plan to integrate it into my application. What are the benefits of using RxJava over EventBus? EventBus separates classes from traditional callbacks and communication occurs through EventBus. EventBus also provides the ability to handle streams by providing options for subscribing to a main or work stream. What additional features are provided by Rxjava compared to EventBus?

+7
android
source share
4 answers

These two are completely different topics / technologies.

An event bus is simply a JMS system for exchanging services using a publisher / subscriber template in ActiveMQ or tibco.

And RXJava is an extension for applying the observer pattern asynchronously if you need to.

As you can see, both are completely different solutions for different needs.

You can read some good documentation in the Vertx Tool that implements EvemntBus http://vertx.io/docs/vertx-core/java/#event_bus

And for RxJava, you can see some practical examples in this project about how this library and its operators work. https://github.com/politrons/reactive

Welcome to the jet world!

+3
source share

Although I am also a beginner, and I have not written any piece of code in RxJAva yet, I will do my best to explain it to you.

The main difference between the two is that RxJava means Reactive Extensions for Java, which means nothing but writing Java code in reactive mode, where reactive is a programming paradigm such as object-oriented programming, and it is based on functional programming. It presents all incoming and outgoing data as a stream of events.

To make this clearer, take a look at the classic Hello World implementation using RxJava:

 public static void hello(String... names) { Observable.from(names).subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println("Hello " + s + "!"); } }); } 

From: https://github.com/ReactiveX/RxJava/wiki/How-To-Use-RxJava

and in the definition taken from its official Github page:

JVM Reactive Extensions is a library for compiling asynchronous and event-based using observable sequences for the Java virtual machine.

It extends the observer pattern to support data / event sequences and adds operators that allow you to put together sequences declaratively, diverting attention to things like low-level threading, synchronization, thread safety, and simultaneous data structures.

From: https://github.com/ReactiveX/RxJava

So, if you want to write code in reactive mode, it is recommended to study the object, then functional (there is an excellent introduction course on Coursera or try to learn Kotlin), finally, choose reactive.

EventBus is a small library that simplifies the exchange of data between actions, fragments, streams, services, etc. It lacks the maaaany functions that RxJava provides. Moreover, the event bus can simply be created using RxJava, so if you use RxJava in your project, you will forget about the EventBus or Otto libraries.

Take a look at: http://blog.kaush.co/2014/12/24/implementing-an-event-bus-with-rxjava-rxbus/

So, this is not how you already noticed, choose this or that library. If you only need to specify an event between actions, in most cases you will not use RxJava, since it is rather heavy.

+2
source share

The EventBus library should be used to organize horizontal interaction between Android components or parts of complex algorithms when transferring links to parts affected by algorithms is too expensive. Unfortunately, it breaks encapsulation a bit, because all subscription methods must be publicly available.
The RxJava library was created by Netflix to deliver reactive programming on Android and is a generalization of the Observer design pattern. Its main goal is to present all incoming and outgoing data as a stream of events. The algorithm itself becomes a "pipeline" that displays incoming and outgoing events. Common objects in rxJava: Observable <>, Subject <>, Subscription, Subscriber.
Observables represent the event source, Subject <>, and all derived classes - "Vortex", which can receive some object from regular code and emit it from Observable. A subscriber is a data receiver connected to some Observable. A subscription is a connection between a data source (Observable <>) and the recipient of this data (Subscriber). When the subscription is active, data can flow from the source to the receiver, when you call .unsubscribe () when subscribing, it disconnects the source and receives. The rest of RxJava is loading statements to organize the flow of data from source to destination.
So these two libraries serve a different purpose. It is worth noting that you can organize execution in the background using the rxJava library using the .subscribeOn () and .observeOn () statements (see Docs)

+1
source share

in my experience, the bus does not work well with the life cycle (Android), i.e. events were sent when no one received them, and after the corresponding components were created, the events were long gone.

+1
source share

All Articles