Observable streams

An oservable streams is nothing but an array that is built over time. Instead of being stored in memory, items arrive asynchronously over time. Observables can be subscribed to, and events emitted by them can be listened to and reacted upon. Every reactive microservice should be able to deal with native observable streams of events. An observable allows you to emit values to the subscriber by calling the next() function in the series:

  • Hot and cold observables: Observables are further classified into hot and cold, based on the producer of the subscription. If it needs to be created more than once, it is called a hot observable, whereas if it needs to be created only once, it is called a cold observable. Simply stated, hot observables usually multicast, while cold observables usually unicast. Taking a live example, when you open up any video on YouTube, each subscriber will see the same sequence, from start to end that's basically a cold observable. However, when you open a live stream, you only can view the most recent view and see further on. This is a hot observable, where only a reference to the producer/subscriber is there and the producer is not created from the beginning of each subscription.
  • Subjects: A subject is just an observable that can call the next() method by itself in order to emit new values on demand. Subjects allow you to broadcast values from a common point while limiting the subscription to only one occurrence. A single shared subscription is created. A subject can be termed both an observer and as observable. It can act as a proxy for a group of subscribers. Subjects are used for implementing observables for general purpose utilities such as caching, buffering, logs, and so on.