Reactive Extensions in JavaScript (RxJS) is one of the most proficient libraries in web development today. RxJS offers the observer pattern, a software design pattern that efficiently allows components of an application to react to certain incoming events or streams of data, also known as event-driven code, where incoming data triggers certain functions.

Observables are most useful when receiving and processing multiple values that arrive in an application over time, but equally capable of returning data from an array, the successful resolution of a promise, DOM events and lots more.

Observable in RxJS:

An Observable is a function, with a few particular traits. As it receives an “observer” (i.e., an object with “complete,” “error,” or “next” methods on it) and returns the cancellation logic. Within an application, observables help pass messages between publishers and subscribers. Observables have the upper hand compared to other techniques for event handling, asynchronous programming, and handling multiple values (they don’t start producing data until they subscribe to it).

Similarly, “Subscribe ()” returns a subscription, or the consumer can call “unsubscribe()” to cancel the subscription and put the producer on hold. RxJS offers several functions that can be used to create new observables. These functions simplify the process of creating observables from events such as timers, promises, and so on. For example:

Observable in RxJS example

Subscription in RxJS:

Subscription in RxJS

An Observable instance starts publishing values only when someone subscribes to it. You can subscribe by invoking the “subscribe()” method of the instance, then passing an observer object to receiving the notifications. A Subscription has one method (“unsubscribe()”) that takes no arguments, and it releases whatever data the Subscriber holds.

Subscription in RxJS example

Observer in RxJS:

Observer in RxJS

As stated earlier, the observer is object literal and has “next(),” “error(),” and “complete()” functions. Per the above example, the observer is the object literal we pass to the “subscribe()” function. After the Observable produces values, it informs the observer by calling the “next()” method when capturing a new value or it returns the “error()” if an error has occurred. Until the signal is completed, the Observable we have subscribed will continue to pass values to the observer.

Example of an observer:

Observer in RxJS example

Observable vs Promise:

If you request through the Promise command and wait for a response, you can be sure multiple responses will not return to the same request. When we create a promise, it resolves with some returning value. The Promise always resolves after the initial value is passed to the resolve function. On the contrary, you can resolve multiple values with the Observable until we call the “observer.complete()” function.

Example of Promise and Observable.

Example Promise and Observable

Subject in RxJS:

Subject in RxJS

An RxJS Subject is an essential type of Observable that allows values to be multicast to many Observers. A subject in RxJS is a unique hybrid type that can act as both an Observable and Observer at the same time. RxJS subjects are a few specialized types of subjects like Async subjects, Behavior subjects, and Replay subjects. RxJS provides many operators, but only a handful are used frequently.

Subject in RxJS example

Advantages of RxJS:

  1. RxJS JavaScript library is designed for reactive programming using observables. With observables, you can “watch” the data stream, passively listening for an event. Applications will detect changes instantly in real-time without unnecessary network calls.
  2. RxJS makes it easy for JavaScript developers to write asynchronous code using composable Observables instead of callbacks and Promises.
  3. In Javascript, there are several sources of asynchronicity, and RxJS can make all of them easier to handle. It’s becoming so popular. The sources are:
    • Ajax <- promises solve this
    • Events (DOM, server-side, web sockets)
    • Animations
    • Inputs (microphone, gaming controller, etc.)


RxJS operators are the main character in reactive programming. Observables are the foundation, and the operators are the methods that help us consume Observables properly. RxJS operators are methods used to compose Observables and work on their data streams. RxJS deals with the basics of JS for getting started with projects using angular JS and Node JS.

Share This