Examples
NATS integration
These examples run monoblok next to a real nats-server: forwarding conditioned subjects out, tapping raw subjects in, and replaying a JetStream stream through the rules before going live.
The configuration forms are described in Export, import and JetStream. Each example script starts the nats-server it needs, so you need nats-server and the NATS CLI installed.
Export to a NATS server#
Intermediate The front door pattern in its smallest form: publishers send to monoblok, and one top-level export form names the subjects that are forwarded to the remote server.
;; Export every `telemetry.>` publish to a remote NATS server, and also
;; republish each one locally under a `.echoed` suffix (which, since it
;; matches `telemetry.>` too, the exporter will forward as well).
;;
;; Point `:servers` at a reachable nats-server. The port here matches
;; scripts/bridge-smoke.sh; override it to wherever your remote actually lives.
;;
;; Start a local remote to point at:
;; nats-server -p 14888
;; Then run monoblok against this file:
;; ./build/monoblok --port 4222 --patchbay examples/bridge.edn
;; And watch forwarded messages on the remote:
;; nats -s nats://127.0.0.1:14888 sub 'telemetry.>'
;; Publish into monoblok to drive the export:
;; nats --no-context -s nats://127.0.0.1:4222 pub telemetry.troubledwater 1
(export
:servers ["nats://127.0.0.1:14888"]
:name "monoblok-export-example"
:export ["telemetry.>"])
(on "telemetry.*"
(publish! (subject-append "echoed") payload))Local subscribers are served first and the remote second, so a slow uplink cannot starve local delivery. The rule's .echoed publishes also match telemetry.>, so they are forwarded too: export applies to client publishes and rule publishes alike.
examples/bridge.shTap an existing NATS server#
Intermediate The reverse direction. monoblok subscribes to raw.> on a real NATS server, runs those messages through the rules, and exposes only the conditioned output. Here the clean stream is also exported back to the same server.
;; Tap raw telemetry from a real NATS server, keep raw private to patchbay,
;; and expose only the conditioned output from monoblok. The conditioned
;; `clean.>` stream is also exported back to the same nats-server.
;;
;; The example script starts nats-server on this port:
;; nats-server -p 14889
;; Then it starts monoblok with this patchbay:
;; ./build/monoblok --port 14222 --patchbay examples/import.edn
;; Publish raw input to the real NATS server:
;; nats -s nats://127.0.0.1:14889 pub raw.temp 42.04
;; Subscribe to the processed local stream:
;; nats -s nats://127.0.0.1:14222 sub clean.temp
;; Or watch the exported stream on nats-server:
;; nats -s nats://127.0.0.1:14889 sub 'clean.>'
(import
:servers ["nats://127.0.0.1:14889"]
:name "monoblok-import-example"
:subject ["raw.>"]
:max-pending 64)
(export
:servers ["nats://127.0.0.1:14889"]
:name "monoblok-export-example"
:export ["clean.>"])
(on "raw.temp"
(-> payload-float
(round 1)
(squelch)
(publish! "clean.temp")))Imported messages are private patchbay input. A direct monoblok subscriber to raw.> does not see the imported raw.temp; subscribers see only what rules publish, such as clean.temp. While import is configured, local clients can still subscribe but their PUB commands are rejected.
examples/import.shThe YAML spelling of the same file:
import:
servers:
- "nats://127.0.0.1:14889"
subject:
- "raw.>"
export:
servers:
- "nats://127.0.0.1:14889"
export:
- "clean.>"
on:
- sub: "raw.temp"
pipeline:
- $payload-float
- [round, 1]
- [squelch]
- [publish!, "clean.temp"]JetStream replay#
Advanced monoblok can consume a JetStream durable consumer. On startup it replays the stream's history through the rules before opening its listener, using each message's JetStream timestamp as the clock, then carries on live with the same consumer. Windows, bars and counters are therefore warm before the first client connects.
# JetStream replay example.
#
# Run the full demo:
# examples/jetstream.sh
# JS_PORT=15999 COUNT=1000 examples/jetstream.sh
#
# Or manually:
# export JS_URL=nats://127.0.0.1:15889
# export BRIDGE_URL=nats://127.0.0.1:15890
# nats-server -js -sd /tmp/monoblok-js-store -p 15889
# nats-server -p 15890
# COUNT=1000 PAUSE_EVERY=500 PAUSE_MS=1100 examples/jetstream-populate.sh
# ./build/monoblok --port 14222 --patchbay examples/jetstream.yml
# nats -s nats://127.0.0.1:14222 sub '$LVC.js.>'
#
# monoblok is a JetStream consumer here, not a JetStream server. The source
# subject stays private patchbay ingress unless a rule publishes derived output.
lvc:
- "js.>"
import:
streams:
- servers:
- env: "JS_URL"
subject: "js.sensors.temp"
stream: "SENSORS"
consumer: "monoblok-jetstream-example"
catch-up: true
export:
servers:
- env: "BRIDGE_URL"
origin-header: true
replay-header: true
export:
- "js.>"
on:
- sub: "js.sensors.temp"
do:
- [count!]
- [publish!, "js.metrics.avg20", [round, 2, [moving-avg, 20, $payload-float]]]
- [bar!, {ms: 1000}, $payload-float]
- [if, $replaying, [publish!, "js.replay.last-temp", $payload], [publish!, "js.live.temp", $payload]]
# A few simple variants to try:
#
# Publish only meaningful live movement:
- sub: "js.sensors.temp"
when: [not, $replaying]
pipeline:
- $payload-float
- [deadband, 0.25]
- [publish!, "js.live.deadband"]
#
# Publish a rounded stable signal from both replay and live data:
# - sub: "js.sensors.temp"
# pipeline:
# - $payload-float
# - [round, 1]
# - [squelch]
# - [publish!, "js.metrics.stable"]What to notice:
env: "JS_URL"reads the server URL from the environment when the patchbay loads, so the file holds no addresses.$replayingis true during catch-up and false afterwards. The first rule uses it to send historical values tojs.replay.last-tempand live ones tojs.live.temp; the second rule'swhen: [not, $replaying]guard keeps its deadband output live-only.[bar!, {ms: 1000}, ...]closes one-second bars on event time during replay, because the replay clock is the JetStream message timestamp.replay-header: truemarks exported messages derived from replay withx-monoblok-replay: true, so the remote side can tell history from live output.- Acks follow success. A message is acknowledged only after its rules and downstream publishes succeed; anything else is left for JetStream to redeliver.
examples/jetstream.sh
JS_PORT=15999 COUNT=1000 examples/jetstream.shThe script starts a JetStream server and a plain NATS bridge target, populates 1000 events with a pause halfway (so the replay has enough event-time gap to close a bar), then runs monoblok through catch-up plus one live event.
MIT licensed · © 2026 Alex Reid
Lexvica offers solutions built with monoblok.