Learn About Observables, Observers And Operators of RxJS – Angular
Photo: emitconference

Learn About Observables, Observers And Operators of RxJS – Angular

In this blog, we will learn about some basics of RxJS and its components like Observables, Observers and Operators. So, first of all, what is RxJS?

RxJS (Reactive Extensions For JavaScript) is a library for reactive programming using observables that makes it easier to compose asynchronous or callback- based code (RxJS Docs).

In the above definition of RxJS, you have encountered the word reactive programming, so let me give you a brief example of reactive programming.

What Is Reactive Programming?

Reactive programming is a declarative programming paradigm concerned with the data streams and the propagations of change.

A programming paradigm is a style or way of programming.

Few common programming paradigms includes:

Imperative Programming

Control flow in imperative programming is explicit. Commands show how the computation takes place, step by step. Each step affects the global state of the computation.

Declarative Programming

Control flow in declarative programming is implicit. The programmer states only what the result should look like, not how to obtain it.

As RxJS is a declarative programming language, so for better understanding of this, let’s take a look at below example. I am going to show an example, how below code works in imperative and declarative programming both.

Example

For example: In imperative programming, a = b +c would mean that a is being assigned the result of b + c in the instant the expression is evaluated, and later, the values of b and c can be changed with no effect on the value of a.

On the other hand in reactive programming, which is declarative, the value of a is automatically updated whenever the value of b or c is changed, without the program having to re-execute the statement a = b + c to determine the presently assigned value of a.

So, RxJS is an incredible tool for reactive programming, and today we are going to dive a little deeper into what observables, observers and operators are.

What is an Observable?

An observable is just a function, which packs the data that can be passed around from one thread to another thread. They are created to emit the data synchronously or asynchronously based on their configuration. They are like the supplier of data to their consumer.

Angular use lot of observables in different scenarios. These includes:

  • To make AJAX request and response,Angular HTTP module uses observables.
  • Observables are used in Router and Form Modules to listen for and responds to user-inputs events.
  • The EventEmitter class extends Observable.

To better understand Observables, let’s create a sample code.

Here, in the above example, we have created our custom observable and subscribe it using the three input values, and the result should be the sum of all values passed to it.

What is an Observer?

In simple term, observers are the consumer of the data, which is emitted by observables as a producer. To consume data, observers subscribe to the observables using its subscribe() method. So, whenever observables emit data, all the registered observers received the data in next() callback.

An observer is an object that defines callback methods to handle three types of notifications that an observable can send. These callback methods are: next(), error() and complete().

  • next() method is used for logic processing and computation. It is required.
  • error() method is used to capture any error. It is optional.
  • complete() is used in the last when the process is completed. It is optional.

We have subscribed our observable using .subscribe() method using the .of() operator, passing the values as a parameter. Without subscribe calling, our observable will not emit any value.

What is an Operator?

RxJS provides us numerous operators like map(), filter(), concat() etc. to perform complex manipulation of the collection on the source observables. Operators are just like functions, which takes configuration options, and return a function that takes a source observable.

Few of the RxJS operators are:

Let’s take a small example of operators.

Using pipe() operator, you can combine multiple operators. For example:

The of() method will create and return an Observable from the 1, 2, 3, 4, 5,6 numbers and the pipe() method will apply the filter() and map() operators on each emitted value.

Summary

In this blog, we read about observables, observers and operators used in Angular app. This post is not in details, but give you an overview of all these concepts. I shared links of few famous operators, which are frequently used in any Angular app.

Further Reading

Adesh

Hi, I am Adesh. I am a senior software engineer having more than 12 years of software development experience. I am a full stack developer and interested in writing the technical post on programming. I am currently working in New York City area.

This Post Has One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu
%d bloggers like this: