Mastering the Basics of RxJS

Snehal Vyawahare
4 min readDec 7, 2023

--

RxJS, or Reactive Extensions for JavaScript, is a library that brings reactive programming to JavaScript. It provides a set of tools for working with asynchronous data streams. Reactive programming is a programming paradigm that deals with asynchronous data streams and the propagation of changes.

Here are some basic concepts in RxJS:

Observables:

  • An Observable is a representation of any set of values over any amount of time. It’s like a stream that you can subscribe to, and it can emit multiple values over time.
  • You can think of an Observable as a sequence of events, and you can subscribe to it to react to those events.
import { Observable } from 'rxjs';

const observable = new Observable(observer => {
observer.next('Hello');
observer.next('World');
observer.complete();
});

observable.subscribe(value => console.log(value));

Observers:

  • Observers are the consumers of Observables. They subscribe to an Observable to receive notifications when the Observable emits new values.
  • An Observer in RxJS is an object that defines three methods: next, error, and complete. These methods are called by the Observable to notify the Observer about the stream of events.

Operators:

  • Operators are functions that allow you to transform, filter, and manipulate the data emitted by Observables.
  • Common operators include map, filter, mergeMap (flatMap), switchMap, etc.
import { from } from 'rxjs';
import { map, filter } from 'rxjs/operators';

const numbers = from([1, 2, 3, 4, 5]);

numbers.pipe(
filter(num => num % 2 === 0),
map(num => num * 2)
)
.subscribe(value => console.log(value));

Subscription:

  • When you subscribe to an Observable, you create a Subscription. This Subscription represents the connection between the Observer and the Observable.
  • Subscriptions can be used to unsubscribe from Observables, preventing memory leaks.

Subjects:

  • Subjects are both Observables and Observers. They can be used to multicast events to multiple subscribers.
  • There are different types of subjects, such as Subject, BehaviorSubject, ReplaySubject, etc.

Schedulers:

  • RxJS uses schedulers to control the concurrency of Observable execution. Schedulers determine when and where the work associated with an Observable will be performed.
  • Examples of schedulers include asap, async, queue, and immediate.

Operators for Combining Observables:

  • RxJS provides operators like merge, combineLatest, and zip for combining multiple Observables.

Error Handling:

  • RxJS provides operators like catchError and retry for handling errors in Observables.
import { of } from 'rxjs';
import { map, filter } from 'rxjs/operators';

const source = of(1, 2, 3, 4, 5);

source.pipe(
filter(num => num % 2 === 0), // Filter even numbers
map(num => num * 2) // Double the remaining numbers
)
.subscribe(
value => console.log(value), // Output: 4, 8
error => console.error(error),
() => console.log('Complete')
);

This example creates an Observable from an array of numbers, filters out the odd numbers, and then doubles the remaining even numbers before subscribing to the result.

Photo by Jessica Lewis on Unsplash

Advantages of RxJS:

Declarative Code:

  • RxJS promotes a declarative programming style, making code more readable and expressive. Developers can focus on what should happen rather than how it should happen.

Asynchronous Operations:

  • It simplifies the handling of asynchronous operations, making it easier to work with events, timers, and AJAX requests.

Composability:

  • RxJS provides a rich set of operators that can be easily composed to create complex asynchronous workflows. This allows developers to build powerful and flexible data pipelines.

Event Handling:

  • It is well-suited for handling event-driven architectures, making it easy to manage and respond to a stream of events over time.

Cancellation and Disposal:

  • RxJS introduces the concept of subscriptions, allowing for efficient cancellation of operations. This helps in avoiding memory leaks.

Error Handling:

  • RxJS provides mechanisms for handling errors in a streamlined manner, making it easier to manage and recover from errors in asynchronous operations.

Disadvantages of RxJS:

Learning Curve:

  • For developers unfamiliar with reactive programming concepts, there can be a steep learning curve. Understanding Observables, Subjects, and various operators may take some time.

Overhead:

  • For simple applications or scenarios where reactive programming is not necessary, using RxJS might introduce unnecessary complexity and overhead.

Debugging Complexity:

  • Debugging reactive code can be challenging due to its asynchronous nature. Understanding the sequence of events and pinpointing issues may require additional effort.

Bundle Size:

  • Including the entire RxJS library in a project can contribute to a larger bundle size. This might be a concern for projects where minimizing file size is crucial.

Potential Abstraction:

  • Overuse of reactive patterns may lead to code that is overly abstracted, making it harder for other developers to understand and maintain.

While RxJS offers powerful tools for managing asynchronous data flows, it’s important to weigh the advantages against the potential drawbacks based on the specific needs and characteristics of a project.

Follow me, Snehal Vyawahare, for more awesome articles on JS.

Thanks for your valuable time.

--

--

Snehal Vyawahare

Software Engineer | Frontend Developer | Technical Writer