Examples use concepts from the RxJava implementation of reactive streams, your language may have alternate naming and more/less concepts depending on the implementation
Reactive Streams Spec http://www.reactive-streams.org/ Live data, unknown source size, asynchronous, backpressure aware(consumer buffering) Set of interfaces to interop between systems(language neutral) that adhere to the spec Max utilization of CPU, multi core, parallel processing Networking aware(TCP, UDP, HTTP and WebSockets) serialization/deserialization
Commonly Confused Terms Observable - is an object that emits data Observer - parent class of an object that can see a stream of data Subscriber - an observer that watches the stream of data from an observable, and implements code to deal with the data Subscription - the object of a subscriber who is watching an observable source, can unsubscribe from the source
Observer Pattern - Push Data Model Model streams as synchronous or asynchronous Observables push changes to subscribers Subscribers receive changes from Observables Subscribers implement data manipulation, data reaction, error handling Do work upon subscription, or at the end of a subscription Termination conditions like success, error, or non terminating
Eventbus - Existing Solution Pros: Data transfer between architectural layers/threads, flexible and easy to use Cons: Data transfer between architectural layers/threads, flexible and easy to use Dynamically typed spaghetti code on a bad architecture/understanding Hard to reason about, unknown life time management depending on the pub/sub implementation(static/dynamic) Multiple events, timing issues, chaining events requires state management, cognitive load
Observables Sources - Hot and Cold Observable is the function that connects you to a data producer Hot produces data regardless subscriptions, often in great quantity Cold produces data only when subscribed to, often in fixed amount Both may or may not terminate, thus data can always be streamed.
Specialized Source Types/Classes Single - single return, or error Completable - on complete message, or error *new* Maybe - single or completable, or error
Observers - Subscriptions Handles the onNext, onError, onCompleted methods from an observable source Called on whatever thread it was assigned on Can offer deterministic cancellation if implemented Many observers can be subscribed to one observable, but can receive totally different parts/transforms of the data
Schedulers - Thread Manipulation io - unbounded thread-pool, low cpu use: network, database, file system computation - bounded thread pool per cpu processor, long run/intense tasks immediate - executes work right away on current thread trampoline - queues work on current thread newThread - per call, uncommon due to heavy thread creation costs
Single Context - Functional Composition Combine multiple observables using operators to create a new stream based on composition logic(smaller and more declarative expression) startWith, merge, zip, and, then, when, combineLatest, join, groupJoin, switchOnNext Distributed composition logic, have operations chained together and referenced as a single operation to be applied on the stream of data
Operators - Data Transformation Manipulate streamed data using any kind of operation like: take, combine, switch, skip sources of data, or to create new data streams. buffer - bundle data into N objects and emit the bundle cache - save results for future calls delay - wait N amount of time time before continuing map - apply a function on the data and return the data(can be a new type) zip - combine observables into one new observable
Subject - Both an Observable and Observer Can subscribe to one or more observables, can pass items it observes as retransmissions or new data AsyncSubject - emits the last value only after the observable completes BehaviorSubject - on subscription, emits the most recent data and continues to stream its observed data PublishSubject - emits entire history of data that was streamed before subscription ReplaySubject - emits entire history of data to subscribers and continues, may have a buffer limit
Additional Benefits Once you understand the concept, open source libraries with reactive interfaces Clear code semantic with an understanding of data source, not state Deterministic threading abstraction to build upon, sequence, parallel, async Easier to test by injecting mock values into the stream Common cross domain technological terminology for any project Community of adoption: http://www.reactivemanifesto.org/ Long term knowledge for system and CPU management
Complications Knowledge of Backpressure and how to handle it Subscribing/unsubscribing management for lifetime(else memory leaks) Large API of operators, additional APIs for programming language specific details Scattered and versioned learning resources Language implementation feature disparity(based on community commit effort) Project adoption is difficult, must be done in small phases, to manage complexity Requires a new way of thinking