Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventBus vs RxJava for Android

Tags:

android

I am a newbie to RxJava and planning on integrating it to my app. What are the advantages on using RxJava over EventBus? EventBus decouples classes from traditional callbacks and communication happens over the EventBus. EventBus also provides thread handling capability by giving options to Subscribe on Main or worker thread. What other additional features are provided by Rxjava compared to EventBus?

like image 980
Kaushik R Avatar asked Nov 07 '16 18:11

Kaushik R


People also ask

What is EventBus in Android?

EventBus is an open-source library for Android and Java using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.

What is the difference between RxJava and RxAndroid?

We use different threads in RxJava. A background thread for the network call and the main thread for updating the UI. Schedulers in RxJava is responsible for performing operations using different threads. RxAndroid is an extension of RxJava and it contains the Android threads to be used in the Android Environment.

What is RxBus in Android?

RxBus — an Event Bus Based on RxJava and Optimized for Android | by Anadea | Medium. Open in app.

What is ReactiveX Android?

ReactiveX, also known as Reactive Extensions or RX, is a library for composing asynchronous and event-based programs by using observable sequences. This is perfect for Android, which is an event-driven and user-focused platform.


2 Answers

Those two are completely different topics/technologies.

Event bus, is just a JMS system to communicate services using publisher/subscriber pattern as ActiveMQ or tibco does.

And RXJava is an extension to apply observer pattern asynchronously if you need.

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

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

And for RxJava you can see in this project multiple practical examples about how this library and his operators works. https://github.com/politrons/reactive

Welcome to the reactive world!

like image 99
paul Avatar answered Oct 13 '22 02:10

paul


Although I am also newbie and I haven't written already any piece of code in RxJAva I would do my best to explain it to you.

The main difference between them is that RxJava means Reactive Extensions for Java, which means nothing than writing Java code in reactive way, where reactive is programming paradigm like object-oriented programming and it's based on functional programming. It represents all incoming and outgoing data as stream of events.

To make it more clear, have a look at 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 at definition taken from its official Github page:

Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.

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

So if you want to write code in reactive way it's recommended to learn object, then functional (there's a great introduction course on Coursera or try to learn Kotlin), finally choose reactive.

EventBus is just a small library to simplify communication between Activities, Fragments, Threads, Services, etc. It lacks of maaaany features, which RxJava provides. Futhermore, event bus can be pretty simply created with RxJava, so if you you use RxJava in your project, you would forget about EventBus or Otto libraries.

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

So it isn't as you already notice choose one or another library. If you need only to provide event between Activities, in most cases you would do not use RxJava as it is pretty heavy.

like image 35
piotrek1543 Avatar answered Oct 13 '22 02:10

piotrek1543