Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RxJS in Action: what is the difference between a Subject and Observable?  RSS feed

 
sven pardo
Greenhorn
Posts: 2
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the difference between a Subject and an Observable.
 
Giovanni Montano
Ranch Hand
Posts: 422
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sven pardo wrote:I don't understand the difference between a Subject and an Observable.

A Subject is both an Observable and an Observer.  Is used in situations where is not so easy to making reactive the code. Is not a difficult concept.
 
Pau Daniels
Author
Greenhorn
Posts: 8
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on Giovanni's answer a bit. Yes, a Subject is an Observable and also an Observer, this makes it useful as a leaf point if we are forced to resort to manually pushing events into our streams.
The other place you will mainly find Subjects is in multicasting behavior. Since by default most Observables are unicast, we can use Subjects when we want to share an Observable between multiple Observers/Subscribers without the overhead of running the Observable multiple times. This usually comes in the form of one of the publish* operators so you shouldn't have to use the Subject directly in that case, but it is always good to understand what is happening under the hood so to speak.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!