RxJS Subjects and Multicasting Operators

RxJS Subjects are a source of confusion for many people using RxJS. For many, the Subject is the obvious and only answer to every problem. Subjects are incredibly useful and necessary, but the key is to know when to use them for solving specific problems that you encounter.

In this course, we are going deep into RxJS Subjects and multicasting operators. You'll gain a solid understanding of when, where, and how to use these powerful tools.

If you are new to RxJS, or want a refresher please checkout this Introduction to Reactive Programming.

Watch User Created Playlist (13)

This playlist is user created.

pro-course-rss-logo

PRO RSS Feed

An Observable execution may only have one Observer

Subject: an Observable and Observer hybrid

Using a Subject as an Event Bus

P

BehaviorSubject: representing a value over time

P

ReplaySubject: remembering events from the past

P

AsyncSubject: representing a computation that yields a final value

P

Connection operator: multicast and connect

P

Stopping a shared observable execution

P

RefCount: automatically starting and stopping an execution

P

Multicasting shortcuts: publish() and variants

P

Reusable multicasting with Subject factories

P

Multicast with a selector argument, as a sandbox

P

Conclusion: when to use Subjects

P
rxjs tutorial about An Observable execution may only have one Observer

An Observable execution may only have one Observer

2:13 rxjs

Let's remind ourselves of a fundamental behavior of the Observable in RxJS, which is: each Observable execution has only one Observer. This means we cannot directly share one single Observable execution to multiple Observers.

rxjs tutorial about Subject: an Observable and Observer hybrid

Subject: an Observable and Observer hybrid

4:06 rxjs

This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as a bridge between the source Observable and multiple observers, effectively making it possible for multiple observers to share the same Observable execution.

rxjs tutorial about Using a Subject as an Event Bus

Using a Subject as an Event Bus

3:57 rxjs PRO

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.

rxjs tutorial about BehaviorSubject: representing a value over time

BehaviorSubject: representing a value over time

3:31 rxjs PRO

Whenever we have multiple observers sharing the same execution, some of those observers may arrive "too late". This lesson will teach you how and when to use a BehaviorSubject to mitigate that problem.

rxjs tutorial about ReplaySubject: remembering events from the past

ReplaySubject: remembering events from the past

5:41 rxjs PRO

A BehaviorSubject can remember the latest value emitted, but what if we wanted Observer B to see all the previous values emitted in the past? We can't do that with BehaviorSubject, but there is ReplaySubject, which allows us to do that. This lessons teaches you everything you need to know about ReplaySubjects.

rxjs tutorial about AsyncSubject: representing a computation that yields a final value

AsyncSubject: representing a computation that yields a final value

4:02 rxjs PRO

This lesson will teach you about AsyncSubject, another type of Subject with some replaying logic inside. We will also look at some use cases for this peculiar RxJS subject variant.

rxjs tutorial about Connection operator: multicast and connect

Connection operator: multicast and connect

5:14 rxjs PRO

We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple observers. However, this technique requires some laborious setting up. In this lesson we will learn about the multicast() operator which helps solve the same problem with less code, and with a neater API.

rxjs tutorial about Stopping a shared observable execution

Stopping a shared observable execution

2:39 rxjs PRO

ConnectableObservable has the connect() method to conveniently dictate the start of the shared execution of the source Observable. However, we need a mechanism to dictate the stop of the shared execution, otherwise a leak happens. This lesson will teach you how to do that, and it's all about Subscriptions.

rxjs tutorial about RefCount: automatically starting and stopping an execution

RefCount: automatically starting and stopping an execution

4:45 rxjs PRO

With the connect() method on a ConnectableObservable, the programmer is responsible for avoiding leaked executions of shared RxJS Observables. This lesson will teach you about refCount(), a handy operator that creates an automatically connected Observable, to avoid manually using connect().

rxjs tutorial about Multicasting shortcuts: publish() and variants

Multicasting shortcuts: publish() and variants

4:21 rxjs PRO

Because using multicast with a new Subject is such a common pattern, there is a shortcut in RxJS for this: the publish() operator. This lesson introduces publish() and its variants publishReplay(), publishBehavior(), publishLast(), share(), and shows how they simplify the creation of multicasted Observables.

rxjs tutorial about Reusable multicasting with Subject factories

Reusable multicasting with Subject factories

5:19 rxjs PRO

The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusable if the shared execution happens to complete or emit an error. In this lesson we will see how to use a simple Subject factory function in order to create a new Subject, one for each shared execution, whenever connect() is called.

rxjs tutorial about Multicast with a selector argument, as a sandbox

Multicast with a selector argument, as a sandbox

7:11 rxjs PRO

Let's explore a different use of the multicast() operator in RxJS, where you can provide a selector function as a sandbox where the shared Observable is available.

rxjs tutorial about Conclusion: when to use Subjects

Conclusion: when to use Subjects

2:36 rxjs PRO

As a conclusion to this course about RxJS Subjects, lets review when and why should you use them.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?