monoblok docs
Docs menu · Export, import and JetStream

Server

Export, import and JetStream

monoblok is meant to be paired with a real NATS cluster. export forwards chosen subjects out, import feeds remote subjects in as rule input, and JetStream import replays history before going live.

For example, sensors can publish to a local monoblok that rounds and filters readings before forwarding selected subjects to a remote NATS cluster. Local subscribers can also receive the processed output.

Both directions connect through vendored nats.c, so TLS and .creds files are supported and nats.c handles reconnects. Leaving the forms out makes the runtime cost zero. Runnable versions of everything here are in NATS integration examples.

Export#

Zero or one (export ...) form in the patchbay file configures the outbound forwarder. The old (bridge ...) spelling is deprecated but still accepted.

EDN
(export
  :servers  ["tls://connect.ngs.global:4222"]
  :creds    "/etc/monoblok/ngs.creds"
  :tls      true
  :name     "monoblok-prod-1"
  :origin-header true
  :replay-header true
  :export   ["telemetry.>" "alerts.>"])

A local publish (from a NATS client or a patchbay rule) whose subject matches any :export filter is forwarded as-is. With :origin-header true, forwarded messages also carry x-monoblok: <hostname> for remote-side provenance, including replay-derived publishes bridged during JetStream catch-up. With :replay-header true, replay-derived bridge publishes carry x-monoblok-replay: true and x-monoblok-assumed-ts: <unix-ms>, the wall timestamp used for replay evaluation; after catch-up, live bridge publishes do not carry either replay header. Local subscribers are served first, bridge second, so a slow remote can't starve local delivery. Reconnects are handled inside nats.c.

Export does not route remote replies back. To store processed prices in a remote JetStream stream, include T.*.p.stable in the export filters and configure the stream to capture that subject. Raw prices remain local unless exported separately.

Export keywords#

A single optional (export ...) form at top level configures the outbound NATS forwarder. Deprecated (bridge ...) is still accepted as a compatibility alias. Counters remain $STATS.bridge.published / .dropped on the normal stats tick.

String-valued config fields can use (env "NAME"); YAML uses env: "NAME". Env values are read once at patchbay load time, must be non-empty, and are not comma-split.

keywordtypemeaning
:serversvector of stringsserver URLs (nats:// or tls://). e.g. ["nats://a:4222" "nats://b:4222"]
:namestringclient name shown in remote monitoring
:credsstringpath to a .creds file (JWT+NKey)
:user / :passwordstringsbasic auth
:tokenstringbearer token
:tlsboolrequired for tls:// URLs
:tls-castringCA cert path (PEM)
:tls-cert / :tls-keystringsclient cert + key (mTLS)
:tls-skip-verifybooldev only, insecure
:origin-headerboolwhen true, forwarded messages include x-monoblok: <hostname>
:replay-headerboolwhen true, replay bridge output includes x-monoblok-replay: true and x-monoblok-assumed-ts: <unix-ms>; live output omits both
:connect-timeout-ms / :ping-interval-msnumberstuning
:max-reconnectnumber-1 for unlimited
:reconnect-wait-msnumbertuning
:exportvector of stringssubject filters to forward (standard NATS wildcards). e.g. ["telemetry.>" "alerts.>"]

Import#

Zero or one (import ...) form configures inbound tap mode. The flat shape is a compatibility alias for one core NATS import:

EDN
(import
  :servers ["nats://raw.example:4222"]
  :name    "monoblok-import-prod-1"
  :subject ["raw.>" "replay.>"])

(on "raw.temp"
  (publish! "clean.temp" payload))

Imported messages are patchbay inputs only. Direct monoblok subscribers do not see the raw imported subject unless a rule republishes it: a subscriber to raw.> will not see the imported raw.temp, only what rules emit, such as clean.temp above. In import mode the local socket stays open for SUB, UNSUB, PING and LVC and stats reads, but client PUB commands are rejected.

Newer configs can make the import kind explicit. :core entries are live core NATS subscriptions; :streams entries are JetStream durable consumers:

EDN
(import
  :core
  [[:servers ["nats://raw.example:4222"]
    :subject ["raw.>"]]]

  :streams
  [[:servers ["nats://js.example:4222"]
    :subject "sensors.temp"
    :stream "SENSORS"
    :consumer "monoblok-prod-1"
    :catch-up true]])

When export and import touch the same cluster, set :origin-header true on both: exported messages carry x-monoblok: <hostname>, and import ignores messages carrying that header, so monoblok does not consume its own output.

Import keywords#

A single optional (import ...) form at top level configures inbound tap mode from a real NATS cluster. The flat shape configures one core NATS import; newer configs can use :core [...] for core NATS entries and :streams [...] for JetStream durable consumers. Imported raw messages run through patchbay but are not visible to direct monoblok subscribers unless a rule republishes them. Local socket clients may still subscribe, but client PUB commands are rejected while import mode is configured.

Counters land on $STATS.import.received, .processed, .dropped, and .failed on the normal stats tick.

Connection keywords match export: :servers, :name, :creds, :user / :password, :token, :tls, :tls-ca, :tls-cert / :tls-key, :tls-skip-verify, :connect-timeout-ms, :ping-interval-ms, :max-reconnect, and :reconnect-wait-ms.

keywordtypemeaning
:subject / :subjectsstring or vector of stringsremote subject filters to subscribe to
:corevector of entry vectorsscoped core NATS import entries
:streamsvector of entry vectorsscoped JetStream import entries; one subject filter per entry in v1
:streamstringJetStream stream name, required inside :streams entries
:consumerstringJetStream durable consumer name, required inside :streams entries
:catch-upboolJetStream historical replay before listener open; default true
:origin-headerboolwhen true, imported messages carrying x-monoblok are ignored
:max-pendingnumberbounded NATS-to-loop queue length; default 4096

JetStream#

With :streams, monoblok consumes JetStream; it does not serve JetStream to local clients. On startup it can replay historical messages before opening its listener, using JetStream message metadata as the event-time clock, so windows, bars, counters and the last-value cache are warm before the first client connects.

JetStream import is consumer-only: monoblok does not serve JetStream to local clients. On startup, :catch-up true replays each configured stream entry to the stream's startup high-water sequence before monoblok opens its listener. Multiple stream entries catch up serially in config order. After catch-up, the same durable consumers continue in live mode. :catch-up false skips the historical event-time loading phase and consumes any durable backlog as live processing-time work.

For filtered stream entries, the startup high-water is a global stream sequence, not a count of messages monoblok must consume. Catch-up stops when a delivered matching message reaches that startup sequence, or when the durable consumer reports pending=0 for the filter.

Replay-derived output is ordinary patchbay output: if it matches export, it is bridged during catch-up. With origin-header: true, those bridged messages carry the same x-monoblok provenance header as live bridged messages. With replay-header: true, replay-derived bridged messages also carry x-monoblok-replay: true and x-monoblok-assumed-ts: <unix-ms>, where the timestamp is the wall timestamp used for patchbay evaluation. Live bridged messages do not carry these replay headers.

Telling replay from live#

During JetStream catch-up, rules see replaying? as true. After the listener opens, replaying? is false. Rules that do not mention replaying? produce the same subjects during replay and live operation; rules that want to suppress or redirect historical output gate that behaviour explicitly:

EDN
(on "js.sensors.temp"
  (if replaying?
    (publish! "js.replay.last-temp" payload)
    (publish! "js.live.temp" payload)))

Acknowledgement#

Event time comes from JetStream message metadata. A message is acknowledged only after the patchbay handler and the downstream publish path succeed. Malformed subjects, oversized payloads, missing metadata, failed rule evaluation, or a failed downstream publish leave the message unacked; JetStream owns redelivery.

The JetStream worker threads never touch rule state. Each message is handed to the single event loop for evaluation, one at a time, and that handoff is backpressured.

See the JetStream replay example for a complete runnable file.

Secrets and addresses from the environment#

Top-level config string values may be written as (env "NAME"). This is load-time only, not a rule-body form, and it reads exactly one environment variable as one string. Missing or empty variables fail validation; values are not split on commas.

EDN
(export
  :servers (env "NATS_SERVERS")
  :token   (env "NATS_TOKEN")
  :export  ["sensors.>"])

In YAML config, write the same value as env: "NAME":

YAML
export:
  servers:
    env: "NATS_SERVERS"
  token:
    env: "NATS_TOKEN"
  export: ["sensors.>"]

MIT licensed · © 2026 Alex Reid
Lexvica offers solutions built with monoblok.