liveBook · Manning

Reactive stream

As explained in Functional Design and Architecture: Examples in Haskell

Reactive Streams is a standard for asynchronous stream processing with non-blocking backpressure between producers and consumers.

A reactive stream models a continuous, time-ordered flow of events. Instead of treating each reading or update as an isolated command, it treats changing data as something that can be observed and reacted to over time. This makes a useful fit when an application must stay alive, accept new environmental data, and produce new results continually. section 2.1.2 section 8.1

Streams support data-flow reasoning: events can be mapped, merged, divided, or zipped into new streams, while a change—or an absence of a value—can trigger an action. Functional reactive programming uses related ideas through time-varying signals; reactive streams instead emphasize a continuing sequence of events and composable transformations. section 2.1.2 section 2.1.2

A stream-oriented design separates recurring input from the work performed when input arrives:

  • Produce readings repeatedly, such as polling a thermometer once a second. section 3.1.4
  • Consume each reading in a monitor that stores it, validates it, logs failures, and raises alarms. section 3.1.4
  • Keep one-off calculations, such as Celsius-to-Kelvin conversion or temperature validation, as callable operations within the flow. section 3.1.4

The contrast is architectural. A sequential scenario can bundle reading, conversion, storage, validation, logging, and alarming into one imperative script. A reactive stream exposes the recurring reading flow, then lets a separate monitor react to each emitted value. The resulting pieces can be composed around interoperability boundaries, such as device input and data transformation, to form a reactive domain model. section 3.1.4 section 3.1.4

Temperature-monitoring flow

Create a thermReadings stream for an outside thermometer. Each second it reads the device and yields a timestamped reading. A thermMonitor stream receives each reading, stores it, validates whether the Kelvin temperature is within bounds, and yields either success or a failure after logging and raising an alarm. The producer does not need to contain all monitoring decisions; the monitor owns that reaction. section 3.1.4 section 3.1.4

Reactive streams belong among several ways to organize reactive applications. Actors and custom event-driven models can also structure systems that must respond to new data. The stream approach is especially suited to expressing transformations and reactions over a continuing flow rather than calculating one final result. section 8.1 section 2.1.2

Listing 3.4 Stream-based scenario
Stream: thermReadings              #1
Description: temperature stream
Given: outside thermometer @therm  #2
Invocation: once a second          #3
  
  readThermometer(@therm),         #4
    Result: @reading
  Yield @reading                   #5

Inspect the stream declaration to see the producer role isolated: periodic device reads are turned into yielded readings before monitoring logic is attached elsewhere. section 3.1.4

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines event streams and their transformations, then sketches a producer-monitor workflow rather than an implementation.
Examples
Uses periodic thermometer readings, Kelvin conversion, validation, storage, logging, and alarms in paired streams.
What you can do after
Apply it to separate recurring input from validation, persistence, and notification reactions.
How it connects
Connects reactive streams with functional reactive programming, signals, data flow, actors, and event-driven models.

Also explained in

Compare how 4 books explain reactive stream →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.