RxJS Subjects - The complete know-how

Understanding how RxJS Subjects contribute to Component Interaction in Angular.

Nidhi
Globant
5 min readJan 27, 2021

--

Image by Goumbik from Pixabay

There are various ways of passing data between components in Angular. In this article, we will be looking at component interaction using Shared Services and RxJS Subjects.

In case if you don’t know about Services, Service is a class in Angular which is decorated with @injectable decorator. It serves two main purposes:

  1. To pass data between different components.
  2. To separate certain processing tasks such as network calls and their error handling, logging directly to the console etc from component.

What is an RxJS Subject??

A Subject is a special kind of Observable from the RxJS library which allows us to multicast values to the components which have subscribed to it. Whenever Subject emits a value, each of its subscribers gets notified about the emitted value.

It has three methods:

  1. next(value)- This method is used to emit a new value.
  2. error(error) - This method is used to send error notifications.
  3. complete()- This method suggests that the Subject has completed itswork. Once complete() method is invoked, calling next() or error() won’t have any effect.
Credit: https://giphy.com/

We will create a small application where:

  1. The user will create a post (create-post component).
  2. Upon submission, post will be displayed in posts component and count will be incremented and reflected in posts-count component.
Snapshot of application

Let's create a new application using the below command.

We will have three components.

  1. create-post — This component will contain a form where the user will enter the details.
  2. posts — This component will display all the posts
  3. posts-count — This component will display total posts count.

We will also create a Shared Service.

Use the below commands to create the components and service using Angular-CLI

Shared Service

  1. Import Subject from ‘rxjs’.
  2. Create a posts Subject using new Subject().
  3. Write a method setPosts for emitting the value using next() method.

create-post component

This component has a reactive form for capturing the user input. Upon submission, we are calling the service’s setPost method to emit the post.

TS

HTML

posts-component

In this component, we will call getPosts method of the Shared Service. Since getPosts method is returning a Subject, we will subscribe to it for setting up the subscription.

Every time posts Subject emits the post, we will push the post value to posts array.

In ngOnDestroy() lifecycle hook, we are unsubscribing from the subscription. If we don’t unsubscribe before the component gets destroyed, it can cause memory leaks.

TS

HTML
Using ngFor directive, we will render the posts.

posts-count component

This is same as the posts component. With each emitted value, we are incrementing the posts counter.

TS

HTML

Credit: https://giphy.com/

Remember with normal Subject, subscribers will only get the values which are emitted after they have subscribed. If you want to get last emitted values, you can use BehaviorSubject or ReplaySubject.

Subject has three types. Lets look at them one by one.

BehaviorSubject
BehaviorSubject stores the last emitted value. When a new user subscribes, he will immediately get the stored value. While creating BehaviorSubject, we have to provide initial value.

Syntax:

ReplaySubject
ReplaySubject can emit multiple old values to new subscribers. While creating the ReplaySubject, you have to specify, how many old values you want to store and for how long you want to keep them.

Syntax:

AsyncSubject
AsyncSubject is different when compared to BehaviorSubject and ReplaySubject. It only emits the last value and that last value is emitted when its execution gets completed.

Syntax:

Let’s Summarize !!

In a nutshell, Subjects are useful for multicasting values to different components in Angular. Whenever the Subject emits a value, components get notified. BehaviorSubject, ReplaySubject and AsyncSubject are three variants of Subject.

next(value) , error(error) and complete() are three methods associated with Subject which are used to emit a value, send error notification and execution completion notification respectively.

Hope the above information helps you in getting a clear picture of Subjects !!

--

--