Stream Oriented Programming is a novel programming paradigm evolving from Reactive, Declarative and Dataflow programming principles in which, reactive streams are a central part of software design.
Streams themselves can be created using a variety of imperative or functional primitives, such as Observables, Signals, Callbags, Callforwards, plain functions, to name a few.
Observables are by far the most mature and feature-rich primitive for Reactive or Stream-Oriented programming, on their way to become a web standard.
The power of composition
Reactive Streams are infinitely composable: group them together in larger streams and use those as higher-level pieces of reusable functionality that exposes the same interface, talks the same language and observe the same contract: data in — data out.
If each of your applications implements the same unified compositional model, it becomes trivial to build large, complex applications that display impeccable quality and maintainability properties in addition to a sharp reduction in the lines of (mostly redundant) code you'll have to deal with.
What is a Stream, exactly?
A reactive stream isn't any magic construct, just a robust and well designed pattern.
Streams get data/events in, optionally perform some processing and then optionally emit some data out.
This could be either sync or async, doesn't matter.
Stream can also combine, mix-and-match data from other streams.
Some perfect examples of Streams are the Subject
and BehaviorSubject
used in RxJS.
Use cases
Big Data pipelines have done this since the age of scalability in a more or less informal way.
Cloud-native development is an example of stream-oriented programming, just generally at a high-enough level to be more of an architectural concern.
UI development is an excellent candidate for this paradigm because everything naturally fits a stream: from clicks on a button which you turn into text on a webpage to navigation, views, sequences of dialog boxes (wizards), to the most complex workflows you can think of.
A mouseover is a stream, a dialog box is a stream, a whole app is a stream.
Most UI libraries and frameworks are conceived for plain imperative programming, so this paradigm is novel, especially in the web development space.
How Streams make code simpler
Reactive streams, within an ecosystem, need to all speak the same language. That helps eliminate one of the most error-prone parts of an application, which is the glue-code that normally binds modules, functions or objects together:
// glue code
target1.data = source1.data
target2.setData(source2.data)
source3.on('data', data => target.doSomethingWith(data))
The beauty of reactive streams is that you can get rid of this clutter forever, connect point-free streams with the certainty they will just work, as type will be the only thing you'll need to match: output type of the source = input type of the destination.
A large surface of bugs comes from imperative steps just moving data from source to destination.
Working with Streams
Working with reactive streams starts with defining them first and then letting the platform or framework do all the necessary bindings, declaratively defined in your template, so that your streams can start interacting with the world:
const stream1 = SomeStream();
const stream2 = SomeStream();
const stream3 = SomeStream();
const template = rml`
<button onclick="${stream1}">click me</button>
<div>${stream2}</div>
<div class="${stream2}" onmousemove="${stream3}">
${stream3}
</div>
`;
The above is the core of the concept: reactive streams first, combined together in a template that declares their bindings and effects to the outside world.
Streams in action
To see streams in action, check out Rimmel.js, the first Stream-Oriented UI library for JavaScript.
Looking for examples? This Stackblitz collection or this collection of examples and experiments I published on this topic illustrate streams-oriented programming in over a hundred examples.
Stream-Oriented Programming is still taking shape, and the best ideas often come from real developers experimenting in the wild. We’ve set up a Discord community where early adopters share snippets, ask questions, and shape the upcoming SOP e-book together. If you want to be part of the crew that defines this paradigm from the ground up, join us here .
Happy coding!
Top comments (0)