Reactive Streams are so simple

Mohan Ram
codeburst
Published in
5 min readAug 26, 2018

Let me start with a simple comparison, imagine stream as a tunnel which helps the consumer to get the data from the producer.

Imagination — Streams as tunnel

Purpose of this article is to cover why and what is streams, whats the mechanism goes under the hood.

Agenda

  1. Why Stream
  2. What is Stream
  3. Core concepts of Stream

Why?

When we need to design our code where data needs to be reactive

consider the below example

Reactivity explained

Hope this makes clear the why part.

What?

Stream is a facilitator which help us to connect producer with the subscriber and they holds the value over a period of time.

Stream is built on top of Observer and Publisher/Subscriber pattern

Observer Pattern
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and automatically notifies them of any state changes (usually by calling one of their methods).

Publisher/Subscriber Pattern
Publish & Subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers.

Observer and Pub/Sub conceptually similar with a key difference below

In Pub/Sub pattern the publisher and subscriber don’t know about the existence of one another.

There is a third component, called broker or message broker or event bus, which is known by both the publisher and subscriber, which filters all incoming messages and distributes them accordingly.

Image source: developers-club

Core concepts

There are many concepts involved in building the stream, lets talk on each.

  1. Observer
  2. Observable
  3. Hot vs Cold Observable
  4. Subject Variants

Observer

Consider Observer as an interface/object which will be registered with streams to get the subscription.

Observer object’s blue print

Observable

Observable is a communication tunnel we talked about.

Observable is a function that tie an observer to a producer.
It is an object class which send notification to the subscriber by calling methods in the observer.

Abstract implementation of Observable for understanding

While initiating an Observable, a subscription function is passed as constructor parameter. Usually that subscription function it self act as producer in many case.

Operators are observable that operate on a source observable.

Consider the example on usage

Creating stream — using observable, observer and observable operator

We created a subscription function which holds the producer logic inside and passed as a constructor argument to create a stream.

Once stream is subscribed, producer is emitting the data.

Hot vs Cold Observable

Observable mode depends on where your producer is created and activated.

COLD is when your observable creates/activates the producer (Producer created *inside* the observable)

Cold Observable

HOT is when your observable listens to created/activated product (Producer created *outside* the observable)

Hot Observable

Subject Variants

We are going to discuss on all the variants of Subject. They are

  1. Subject
  2. Behavior Subject
  3. Replay Subject
  4. Async Subject

Subject

Subject is both an Observable and Observer.

  1. Object which facilitate us to push the data into stream and subscription object receives the same.
  2. Subject shares the same observable (In this case, Observable is Hot)
  3. It acts as a bridge/proxy between the source observable and many observers, making it possible for multiple observers to share the same observable execution.
Abstract implementation of Subject for understanding
example usage of Subject

Behavior Subject

Its a kind of subject with following ingredients

  1. Object initialization needs an initial value as it always returned as a value on subscription.
  2. Upon subscription it returns the last value of the subject.
  3. At any point, you can retrieve the last value of the subject using the getValue() method.
Abstract implementation of Behavior Subject for understanding
example usage of Behavior Subject

Replay Subject

Kind of subject with the following behaviour

  1. Object initialization is done with the replay buffer size.
  2. when a future subscription comes, considering the replay buffer size those old/past values are emitted back from the buffer maintained internally.
Abstract implementation of Replay Subject for understanding
example usage of Replay Subject

Async Subject

Subject variant that sends only the last value of the Observable execution to all the subscribers, and only when the execution completes.

Abstract implementation of Async Subject for understanding
example usage of Async Subject

Conclusion

In short, Stream is a mediator between Producer and Consumer.

Pretty much, I tried to cover up all the core concepts which roams under the hood of reactive stream.

You can find the complete code base in GitHub from here.

Hope it helps the community 😃

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

Sign up to discover human stories that deepen your understanding of the world.

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

No responses yet

Write a response