Ramya S
Ramya S

Reputation: 3094

Only to pass messages to sibling component should i use subject or Observable?

 const simpleObservable  = new Observable( (observer) => {
     observer.next("My message");
     observer.complete();
 })

To subscribe in sibling component,

 simpleObservable.subscribe();

For subject,

const subject = new Rx.Subject();

subject.next("My message");

subject.subscribe((data) => {
      console.log(data);
})

I know that main reason to use subject is for multicast and observable is unicast( each subscribed observer owns independent execution of observable).

So for just passing messages, which is more efficient and good to use?

Upvotes: 0

Views: 78

Answers (1)

v8-E
v8-E

Reputation: 1105

It really depends on what you want to do with it. I suggest go for uni-casting because in multicast operation observable executes just once, and not on a per subscription basis. If the underlying observer completes, the middleman subject also completes , so any subscribers that get added in the future will not receive any data or simply

once a subject completes, future subscribers to the subject will not receive more data

also note that

    • An observable, by definition is a data producer, special kind that can produce data over time.
    • A Subject on the other hand can act as both data producer and data consumer.

Upvotes: 1

Related Questions