Member-only story

Understanding rxjs Subjects

Luuk Gruijs
2 min readApr 17, 2018

Rxjs is great. It helps you with composing and subscribing to data streams. You probably do this a lot with “plain” Observables. Rxjs however offers a multiple classes to use with data streams, and one of them is a Subject.

If you think you understand Observables, read on! Else i suggest you to read more about it in my other article: Understanding, creating and subscribing to observables in Angular.

Subjects

A Subject is like an Observable. It can be subscribed to, just like you normally would with Observables. It also has methods like next(), error() and complete() just like the observer you normally pass to your Observable creation function.

The main reason to use Subjects is to multicast. An Observable by default is unicast. Unicasting means that each subscribed observer owns an independent execution of the Observable. To demonstrate this:

While Observables are unicast by design, this can be pretty annoying if you expect that each subscriber receives the same values. Subjects can help us overcome this issue. As mentioned…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Luuk Gruijs
Luuk Gruijs

Responses (32)

What are your thoughts?

Simple and concise. This is the best article about Subject I’ve read. Thanks.

Simple introduction, but all these are basically coverd in rxjs overview tutorial: http://reactivex.io/rxjs/manual/overview.html#subject