Notes about reactive programming — RxJS6*

  • This is a copy of the notes I took for myself, when I started learning about RxJs, I hope someone finds them useful.

Types of observables

  1. ‘Unicast’ observables
  2. Subject(multi-cast observable)
  3. Behavior subject
  4. Replay subject

etc. <Only the most common types listed>

  • To start using reactive programming, install the rxjs library as follows:

Alternatively, include the rxjs scripts in your index.html

  • It is also worth noting that in Angular 6.* the library is installed by default.

What is an observable?

  • Simply put, an observable represents a stream of data that can be invoked or used asynchronously.
  • The data is then consumed by subscribing (observing) to the observable. This operation returns a Subscription.
  • An observable can emit values even before observers subscribe to it .i.e. It is a Hot observale. Cold observables only emit values when ‘someone’’ starts observing their values.
  • Behavior and Replay subjects are used to re-emit values to observers in case they are ‘late’ to start subscribing.

Working with observables — Code snippets

  • Below are some of the common use cases of observables:
  1. Create and subscribe to observable
  • The code above, will print: 1,2,3 sequentially on the console
  • More examples can be found here.

2. Pre-processing observable using operators

  • It is also possible to manipulate the data returned by observables using rxjs operators.

3. Working with multi-cast observables

  • The following example from shows one way to use multi-casting to allow multiple observers on an obsevable:
  • Read more about observables HERE.
  • Remember: Practice makes perfect!

Written by

Former Glorified Electrician(aka Electrical Engineer). Now a Software Developer working on complex Enterprise Software. Lets connect on twitter @NdamuleloNemakh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store