If you find yourself frustrated about learning RxJS and it seems to be hard for you — you are not alone. At the beginning of my career, I felt the same. In this article, I will reveal some important concepts from RxJS and guide further learning.

If you develop an Angular application, one of the first things you hear about is the RxJS library. And it's true — I haven't seen an Angular application without the RxJS library and it's hard to imagine such an application i.e. if it's not just markup. So the question is: why do you need RxJS for Angular applications? Why is so important? Let's dig into it.

RxJS is a JavaScript library, specifically designed for reactive programming. It is used for managing asynchronous events and data transformation. RxJS has a huge community, and the library constantly improves. It introduces Observables, Subjects, and tons of operators to manipulate and manage data. In web development, we deal with a lot of asynchronous events, so it's crucial to properly manage them effectively to provide a seamless and smooth user experience. This is where RxJS comes in handy.

Pillars of RxJS: Observables, Subject & Operators

What is an Observable?

If you're familiar with the Observer pattern, then you're on the right track. An Observable is essentially an implementation of this pattern. It's a data stream, and you can manipulate it by operators, or get this data by subscribing to it. To understand it, let's get some examples from real life.

Users can click the 'Subscribe' button, and whenever new content is posted, they receive it in their email. This feature, commonly seen on many websites today, perfectly exemplifies the Observable pattern. In this case, the user is an Observer (like a person, who constantly looks at your news feed), and the news content represents the data stream. The user subscribed to the news and it arrives in their email inbox. If at any point the user decides they no longer wish to receive news, they can simply unsubscribe from the stream. In essence, this is how Observables function.

To create an Observable, you need data. This data can vary by its source, resulting in two different types of Observables: hot and cold. If your data is produced by the Observable itself, it's a cold Observable. and when your data is produced outside it - it's a hot one.

Now, back to our sample news website to illustrate this point further.

Consider the process of sending out a daily newsletter to your subscribers. If the newsletter is generated each time a user subscribes, and each subscriber receives a unique copy, this would be an example of a cold Observable. The data (in this case, the newsletter) is produced by the Observable itself upon each new subscription.

On the other hand, suppose there's a live news update feature on your website. When an important news event occurs, the update is instantly pushed to all current users on the website, regardless of when they started observing. This would be a hot Observable because the data is produced outside the Observable (the news event happens regardless of whether anyone is subscribed) and it is shared among all subscribers.

Subject is a special type of Observable, that allows to push the data to their observers.

There are four types of Subjects:

In total, we have 4 Subjects (Subject, BehaviorSubject, ReplaySubject, and AsyncSubject). Understanding the differences between these types of Subjects is crucial for determining which one to use in different scenarios. For instance, a BehaviorSubject might be used when you want to have a value always available (like user authentication status), while an AsyncSubject could be used when you need the final result of a series of computations or a network request, but not the intermediate results.

Operators are an integral part of RxJS, allowing us to transform, combine, manipulate, and work with our Observables in various ways. They are simply functions that take an input Observable, transform it in some way, and return a new Observable. You can chain operators together using the pipe() function on an Observable. The order in which you place your operators in the pipe does matter.

Let's explore some operators with examples.

Caution! Failing to unsubscribe from Observables can lead to memory leaks. This usually happens when an Observable continues to produce values that are no longer needed but are still being consumed due to an active subscription. To prevent this, it's important to unsubscribe when the data is no longer needed. Operators like take and takeUntil can be used to automatically complete an Observable and unsubscribe from it once a certain condition is met. However, remember that not all Observables require manual unsubscribing, particularly those that complete or emit a finite number of values.

Usage examples in Angular

As was mentioned at the start of the article, RxJS is vital for Angular applications, and it's used in several key areas of the framework. Here are some real-world use cases:

This article can help you complete the initial picture of RxJS and its usage. But it's only a starting point. Becoming proficient in technology requires time, effort, and consistency.

These links can help you master the RxJS library: