Skip to main content

NatsConfig Reference

Full field reference for NatsConfig. For usage examples - auth, TLS, environment variables, HOCON - see the Configuration guide.

zio-nats is built on jnats, the official Java NATS client. NatsConfig maps directly onto jnats connection options - if you need a setting that is not exposed here, the jnats documentation is the authoritative source for what the underlying options do.

Constructor

All fields have defaults. Pass only what you want to override:

NatsConfig(
// Connection
servers = List("nats://localhost:4222"),
connectionName = None, // Option[String] - shown in server logs
connectionTimeout = 2.seconds,
reconnectWait = 2.seconds,
maxReconnects = 60, // -1 = unlimited
pingInterval = 2.minutes,
requestCleanupInterval = 5.seconds,
bufferSize = 64 * 1024, // bytes - incoming message buffer
noEcho = false,
utf8Support = false,
inboxPrefix = "_INBOX.",

// Authentication - pick one variant:
auth = NatsAuth.NoAuth,
// auth = NatsAuth.Token("s3cr3t"),
// auth = NatsAuth.UserPassword("alice", "p4ss"),
// auth = NatsAuth.CredentialFile(Paths.get("/app/nats.creds")),
// auth = NatsAuth.Custom(myAuthHandler),

// TLS - pick one variant:
tls = NatsTls.Disabled,
// tls = NatsTls.SystemDefault,
// tls = NatsTls.KeyStore(keyStorePath = ..., keyStorePassword = "..."),
// tls = NatsTls.Custom(mySSLContext),

// Reconnect tuning
reconnectJitter = 100.millis, // jitter added to reconnect wait
reconnectJitterTls = 1.second, // jitter when using TLS
reconnectBufferSize = 8 * 1024 * 1024, // bytes buffered during reconnect; 0 = disabled

// Outbound queue
maxMessagesInOutgoingQueue = 0, // 0 = unlimited
discardMessagesWhenOutgoingQueueFull = false,
writeQueuePushTimeout = Duration.Zero, // Zero = block indefinitely

// Socket tuning
socketWriteTimeout = Duration.Zero, // Zero = OS default
socketReadTimeout = Duration.Zero, // Zero = OS default

// Protocol
maxControlLine = 0, // 0 = jnats default (4096 bytes)
maxPingsOut = 2, // unanswered pings before disconnect

// Lifecycle
drainTimeout = 30.seconds // max time to drain subscriptions on close
)

Field reference

FieldTypeDefaultNotes
serversList[String]List("nats://localhost:4222")Multiple servers for clustering / failover
connectionNameOption[String]NoneShown in server connection logs
connectionTimeoutDuration2.secondsMax time to establish TCP connection
reconnectWaitDuration2.secondsPause between reconnect attempts
maxReconnectsInt60-1 for unlimited
pingIntervalDuration2.minutesHow often to ping the server
requestCleanupIntervalDuration5.secondsHow often to purge timed-out request inboxes
bufferSizeInt65536Incoming message buffer in bytes
noEchoBooleanfalseSuppress messages the connection published
utf8SupportBooleanfalseEnable UTF-8 subject names
inboxPrefixString"_INBOX."Prefix for ephemeral reply subjects
authNatsAuthNoAuthAuthentication method - see below
tlsNatsTlsDisabledTLS mode - see below
reconnectJitterDuration100.millisRandom jitter added to reconnect wait
reconnectJitterTlsDuration1.secondRandom jitter added to reconnect wait (TLS)
reconnectBufferSizeLong8388608Bytes buffered during reconnect; 0 = disabled
maxMessagesInOutgoingQueueInt00 = unlimited outbound queue
discardMessagesWhenOutgoingQueueFullBooleanfalseDrop messages instead of blocking
writeQueuePushTimeoutDurationDuration.ZeroZero = block indefinitely
socketWriteTimeoutDurationDuration.ZeroZero = OS default
socketReadTimeoutDurationDuration.ZeroZero = OS default
maxControlLineInt00 = jnats default (4096 bytes)
maxPingsOutInt2Unanswered pings before disconnect
drainTimeoutDuration30.secondsMax time to drain subscriptions on close

NatsAuth variants

VariantWhen to use
NatsAuth.NoAuthAnonymous server - default
NatsAuth.Token(token)Static token authentication
NatsAuth.UserPassword(user, pass)Username and password authentication
NatsAuth.CredentialFile(path)NKey/JWT credentials file (e.g. Synadia Cloud / NGS)
NatsAuth.Custom(handler)Runtime AuthHandler - for dynamic credentials or Vault

NatsTls variants

VariantWhen to use
NatsTls.DisabledNo TLS - default
NatsTls.SystemDefaultServer has a public CA cert; use the JVM trust store
NatsTls.KeyStore(...)mTLS with JVM keystore and truststore files
NatsTls.Custom(sslCtx)Pre-built SSLContext - for certs loaded at runtime

NatsTls.KeyStore fields: keyStorePath, keyStorePassword, trustStorePath (optional), trustStorePassword (optional), tlsFirst (default false).

Convenience constructors

ConstructorEquivalent to
NatsConfig.defaultNatsConfig() - localhost, no auth, no TLS
NatsConfig("nats://host:4222")NatsConfig(servers = List("nats://host:4222"))
Nats.default (ZLayer)ZLayer.succeed(NatsConfig.default) >>> Nats.live
NatsConfig.fromConfig (ZLayer)Reads all fields from ZIO config (env vars or HOCON)

Environment variable mapping

When using NatsConfig.fromConfig, field names map to NATS_UPPER_SNAKE_CASE:

FieldEnvironment variable
serversNATS_SERVERS (comma-separated for multiple: nats://a:4222,nats://b:4222)
auth.typeNATS_AUTH_TYPE (no-auth, token, user-password, credential-file)
auth.valueNATS_AUTH_VALUE (for token)
auth.usernameNATS_AUTH_USERNAME
auth.passwordNATS_AUTH_PASSWORD
tls.typeNATS_TLS_TYPE (disabled, system-default, key-store)
connection-timeoutNATS_CONNECTION_TIMEOUT (ISO-8601: PT5S = 5s, PT2M = 2min)
max-reconnectsNATS_MAX_RECONNECTS
socket-read-timeoutNATS_SOCKET_READ_TIMEOUT