Skip to content

nats://

NATS messaging on top of nats.go. Pub/sub, queue groups, request/reply, and JetStream persistence. Connection pooling, TLS, multiple auth modes (username/password, token, NKey seed, .creds file), and reconnect are all built in.

URI shape

nats://<subject>?[options]

<subject> is a NATS subject (e.g. orders.created or metrics.*). Wildcards work for consumers; producers should publish on a concrete subject. The servers URI param overrides the component's default server list.

Parameters

Connection

Param Type Default Description
subject path — NATS subject (path token).
servers string nats://localhost:4222 Comma-separated server URLs.
connectionTimeout duration 2s Initial connect timeout.
noEcho boolean false Don't receive own publishes (avoid feedback loops on shared subjects).
noRandomizeServers boolean false Preserve server URL order on reconnect (don't shuffle).
traceConnection boolean false Log connect / disconnect / reconnect events.

Reconnect & liveness

Param Type Default Description
reconnect boolean true Enable automatic reconnect on transport loss.
maxReconnectAttempts integer 60 Max reconnect attempts. -1 = forever.
reconnectTimeWait duration 2s Delay between reconnect attempts.
pingInterval duration 2m Server ping cadence.
maxPingsOut integer 2 Missed pings before force-disconnect.

Auth (mutually exclusive)

Param Type Default Description
credentialsFilePath string — Path to NATS .creds file (JWT + NKey seed).
username string — SASL-style username. Pair with password.
password string — SASL-style password.
token string — NATS auth token (single string).
nkeySeedFile string — Path to a raw NKey seed file.

TLS

Param Type Default Description
secure boolean false Force a TLS handshake (with system trust).
tlsCAFile string — PEM CA bundle.
tlsCertFile string — PEM client cert for mTLS.
tlsKeyFile string — PEM client private key.
tlsInsecureSkipVerify boolean false Skip server cert verification. Insecure.
tlsMinVersion / tlsMaxVersion enum — Pin TLS version (TLSv1.2 / TLSv1.3).
tlsCipherSuites string — Comma-separated Go cipher-suite names.

Producer

Param Type Default Description
replySubject string — Default reply-to header on outbound messages. Per-message nats.reply overrides.
requestTimeout duration 20s Request/reply timeout when the exchange pattern is InOut.
flushConnection boolean true Flush after each publish to ensure delivery before returning.
flushTimeout duration 1s Per-publish flush timeout.

Consumer

Param Type Default Description
queueName string — NATS queue-group name. Load-balances across multiple consumers.
maxMessages integer 0 Auto-unsubscribe after N messages. 0 = unlimited.
replyToDisabled boolean false Suppress automatic reply when an inbound message carries a reply.
poolSize integer 10 Worker-goroutine pool size for processor dispatch.

Headers

Param Type Description
headerIncludePattern string regex Pass-only headers whose name matches. Empty = include all.
headerExcludePattern string regex Drop headers whose name matches (applied after include).

JetStream

Param Type Default Description
jetstreamEnabled boolean false Route through the JetStream persistent subsystem.
jetstreamName string — Stream name. Defaults to subject when empty.
jetstreamAsync boolean true Producer uses PublishMsgAsync.
pullSubscription boolean true Pull (client-driven, backpressure) vs push (server-pushed).
durableName string — Durable consumer name. Server persists subscription state.
deliverPolicy enum all all / last / new / by-start-seq / by-start-time / last-per-subject.
ackPolicy enum explicit explicit / all / none.
maxDeliver integer 0 Max redelivery count. 0 = unlimited.
replayPolicy enum instant instant / original.
ackWait duration — Server wait for ack before redelivering. 0 = library default (30 s).
inactiveThreshold duration — Tear down the durable consumer after this much idle time.

Body shape

Producer

  • In body — published verbatim (BodyAsBytes()).
  • In headers — passed through the include/exclude filter and set as NATS headers (NATS 2.2+ feature).
  • InOut exchanges trigger a request/reply (nc.Request); the reply becomes the In body.

Consumer

  • In body — message payload bytes.
  • In headers:
Header Description
nats.SUBJECT Source subject.
nats.REPLY Reply-to subject (if set).

Plus any pass-filtered NATS headers.

When the inbound message carries a reply and replyToDisabled=false, the processor's output body is auto-published back to that subject.

Examples

Pub/sub consumer:

- id: events
  from: nats://orders.>?queueName=svc&servers=nats://nats:4222
  steps:
    - to: direct://process-order

Request/reply producer (InOut):

- setBody:
    starlang: '"{\"sku\":\"" ++ headers["sku"] ++ "\"}"'
- to: nats://inventory.lookup?requestTimeout=2s&servers=nats://nats:4222
# After: ex.In() holds the reply body.

Auto-reply server (treat the route as an RPC implementation):

- id: lookup
  from: nats://inventory.lookup
  steps:
    - to: postgres://inventory/findOne
    # the body produced here is auto-published to msg.Reply

JetStream durable pull subscription:

- from: nats://orders.>?jetstreamEnabled=true&jetstreamName=ORDERS&durableName=order-processor&pullSubscription=true&ackPolicy=explicit&maxDeliver=5

TLS + .creds auth:

- to: nats://metrics.app?servers=tls://nats.prod:4222&credentialsFilePath=/etc/nats/svc.creds&secure=true&tlsCAFile=/etc/ssl/nats-ca.pem

Behavior notes

  • Connection pooling. Endpoints with identical connection parameters share a single *nats.Conn (connection_pool.go).
  • noEcho. Set on shared subjects where the publisher and subscriber sit on the same conn — prevents your own publishes from echoing back as messages.
  • Auth modes are mutually exclusive. Pick one of credentials file / user+password / token / NKey seed. Mixing them errors at parse time.
  • JetStream defaults. pullSubscription=true, ackPolicy=explicit is the standard durable-worker pattern. Explicit ack is the processor returning nil; on processor error the message is nak'd (redelivered per maxDeliver).
  • Header filter is symmetric. The include/exclude regexes apply on outbound (Helix → NATS) and inbound (NATS → Helix) directions, so one rule governs both sides.
  • Producer flush. flushConnection=true (default) ensures bytes hit the wire before Process returns. Disable for higher throughput at the cost of lost-on-crash semantics.

Source

  • pkg/component/nats/nats.go — Component, Endpoint.
  • pkg/component/nats/{producer,consumer}.go — pub/sub paths.
  • pkg/component/nats/{jetstream}.go — JetStream variants.
  • pkg/component/nats/{config,security,connection_pool,headers}.go — config parsing, TLS+auth, pooling, header filter.

See also