The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Using a Subject as an Event Bus

Using a Subject as an Event Bus

3:57
Every Subject is an Observer, meaning we can freely use the Observer methods next(), error(), complete() to manually send events to the Subject. This lesson will teach you how to use a Subject as an Event Bus through the Observer methods.
Watch this lesson now
Avatar
egghead.io

Every Subject is an Observer, meaning we can freely use the Observer methods next(), error(), complete() to manually send events to the Subject. This lesson will teach you how to use a Subject as an Event Bus through the Observer methods.

Avatar
Brendan

What exactly do you mean by violating the observable contract? Also, why is it called a 'subject'?

In reply to egghead.io
Avatar
Andre

Hi Brendan. The Observable contract means that all observables should agree to never emit 'next' after either 'complete' or 'error' is emitted. It is technically possible to call subject.complete() then subject.next(a), but this would violate that convention, leading to possible bugs or weird behavior. There is a lesson in Egghead about the Observable contract.

A Subject has that name to stay similar to the Observer pattern (a software design pattern) because in that design pattern the "subject" was an object containing a list of observers. In RxJS, the subject is the only type of object that contains a list of observers (while common observables do not have such list internally).

In reply to Brendan
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?