RxJava Relay vs Subjects

subscribe has overloads and, usually, people get used to the subscribe(Consumer) overload. Then they use subjects and suddenly onComplete is also invoked. RxRelay saves the user from themselves who don't think about the difference between subscribe(Consumer) and subscribe(Observer).

  1. Don't forward errors/completions events to the subject:

Indeed, but based on our experience with beginners, they often don't think about this or even know about the available methods to consider.

  1. Don't expose the subject, make your method signature return an observable instead.

If you need a way to send items into the subject, this doesn't work. The purpose is to use the subject to perform item multicasting, sometimes from another Observable. If you are in full control of the emissions through the Subject, you should have the decency of not calling onComplete and not letting anything else do it either.


Subjects have far more overhead because they have to track and handle terminal event states. Relays are stateless aside from subscription management.

- Jake Wharton

(This is from the issue OP opened on GitHub and felt it was a more a correct answer and wanted to "relay" it here for others to see. https://github.com/JakeWharton/RxRelay/issues/30)

Tags:

Rx Java2