I am confused about the difference between EventBus and RxJava in android. I need to implement one of them for my issue about notifying some components when some changes have been done, so that they can update their state.
Also, I read that EventsBus has became deprecated over RxJava and I don't know if this information is true or not.

- 1,440
- 4
- 14
- 27
-
2As far as I know, they serve slightly different purposes, but, basically, RxJava can do much more than EventBus – OneCricketeer Feb 28 '16 at 17:10
-
6Otto is deprecated but GreenRobot is not. http://greenrobot.org/eventbus/ – Kuffs Feb 28 '16 at 17:22
-
I voted to reopen this question, because the linked-to duplicate question doesn't have answers as valuable as the ones in here (the only answer posted there only deals with RxJava). – Daniel F Dec 20 '19 at 13:38
-
@DanielF did you VTC the other as dupe too? – Tiago Martins Peres Jan 09 '20 at 14:28
-
1@TiagoMartinsPeres - No I did not. I would if this one gets reopened. – Daniel F Jan 09 '20 at 15:59
4 Answers
EventBus
and RxJava
are different in their nature.
EventBus
is just a bus
as the name suggest - it provides the mechanism to subscribe and publish events to the "bus", without you caring how the wiring is done, what this "bus" actually is, etc. In the context of Android, the EventBus
is just an easier way to deal with sending and receiving Broadcast
messages with less boilerplate.
RxJava
on the other hand is much much more powerful than that. Yes, you can subscribe and publish events, but you have far more control over the process - frequency, on which thread everything happens, etc. The main power of RxJava
(in my opinion) is that you can manipulate the data being published very easy, using some of its tons of operators
.
To sum up - if you only care about publishing some events and performing some actions when received - you'd probably be better off using the simplest of the two, namely some kind of Bus
, or even plain old BroadcastReceiver
s. If you will also benefit of transforming the data, handling threading or simplified error handling - go for the RxJava
approach. Just keep in mind that RxJava
generally has a steep learning curve, so it takes some time to get used to its concept.
-
Thank's for your answer. what are the disadvantages of using EventBus ? is it limited in comparison to RxJava ? – HiddenDroid Feb 29 '16 at 11:34
-
2@HiddenDroid Yes it is; you could just go ahead with EventBus but in my opinion RxJava has much to offer with its operators. RxJava is gaining popularity (actually not just RxJava, reactive programming in general) and you will find many libraries include it in their APIs (for instance, Retrofit). On a side note, it's really convenient to use it once you get used to it. – mewa Feb 29 '16 at 22:49
-
3@HiddenDroid: Can't think of any specific disadvantages of `EventBus`- it always comes down to what your task is. As I said in my answer - if you only need to send some / respond to some events - the bus will do the job perfectly. If your task is more complicated than that - perhaps it's worth exploring other solutions. The trick is to find the right tool for the task, so no need to push yourself into RxJava if all you need are 1-2 BroadcastReceivers ... – Vesko Feb 29 '16 at 22:52
-
@Vesko Thank's a lot for your answer. I can understand from your that EventBus is not recommended when the task is more complicated. What I really want to understand is, what is the negative factor in the EventBus architecture, so that it doesn't work with complicated tasks ? – HiddenDroid Feb 29 '16 at 23:46
-
1@HiddenDroid, it's not that EventBus is not recommended - it all depends on the task in hand. Comparing both is like comparing oranges to apples - they have different purposes. The power of `RxJava` is in it's operators - check them here: http://reactivex.io/documentation/operators.html. As you can see, you can super easily transform, filter, chain, buffer, handle errors and do so much other stuff. – Vesko Mar 01 '16 at 08:42
To understand RxJava, think of a list. Today manipulating a list like transforming, splitting, merging can be done easily using functional methods (map, groupBy, etc). RxJava uses the same principles except that its main target is not list but stream. Stream is asynchronous, often live data such as websocket channel or online movie.
Event bus comes from the needs to decouple classes which in Android are often bound with life cycle. Tight coupling of network callback and Activity's Views as an instance, has been a cause of numerous null pointer exceptions. Event bus with its publisher-subscriber pattern alleviates this issue.
How does it get mixed with RxJava ? To begin RxJava incorporates Observable pattern. Here an Observer watches an Observable and reacts when an event arrives. Observable has several sub-classes, among which is Subject that has the properties of both Observable and Observer. Since it works by trapping an event and publishing it to subscribers, it technically functions as event bus.
Is it wise to use RxJava as event bus ? No. RxJava would introduce unnecessary complexities for simpler purposes. Only use it if the app does manipulate streams. For example pairing frames from a movie stream and subtitles from another stream. If the app simply consumes a REST API and needs to decouple the callback from activities/fragments then event bus is enough.

- 8,880
- 4
- 47
- 52
Live @Vesko wrote, RxJava and event bus differ in their nature and may serve to solve different problems. Nevertheless, there are some scenarios in which both of them can solve the same problem (although at different costs), and this might be the reason for why many people confuse these two concepts.
RxJava is conceptualy similar to Android LiveData that was released not so long ago, and to better understand these concepts, as well as event bus, I suggest you read my post. In the post I go over these very concepts, describing the scenarios in which we should use one over another and the pros and cons of using one rather than the other. I think it may be useful to you:

- 194
- 1
- 7
If you want to fetch data from sever and update UI, use RxJava + Refrofit. If update UI or do some operation without fetching data, EventBus is enough.

- 49
- 3