RxJS — Subjects

Jawadhasan
7 min readNov 11, 2023
Photo by Jainath Ponnala on Unsplash

Introduction

In previous posts we’ve discussed RxJS and saw few examples to understand how to use those in our angular applications

You can check the earlier posts in following links

Today, we’ll discuss Subject which is very useful and powerful feature of RxJS.

We’ll start simple with few examples mostly around the subject theory and then later we’ll put subject to good use by creating an EventBus and an Observable Service which will allow us to send data and notifications between components.

Let’s start with the basics first.

A key purpose of subject is to send out notifications.

A subject is a special type of observable that can multicast a value or event to multiple subscriber, meaning it provides a way to send one or more data values to listeners. Any component or service that subscribes to the subject’s observable will receive notification.

A subject is also an observer. An observer allow us to push new data into observable sequence. Any component or other services can feed new values into the subject, using its next method. The new value then multi-cast to all subscribers.

There are various type of subjects available, we’ll cover the followings

  • Subject
  • BehaviorSubject
  • AsyncSubject
  • ReplaySubject

What are Subjects

A subject is a special type of observable that implements the observer interface. So it is both an observable and observer.

Following are few characteristics of a subject.

  • Observable
  • Observer
  • Produce values
  • Proxy values
  • Have state and maintains a list of observers.
  • Multicast instead of unicast

--

--

Jawadhasan

Software Solutions Team Lead | Cloud Architect | Solutions Architect