mqutils
GitLab ↗

Docs / Backends / NATS

NATS

NATS Core (at-most-once pub/sub with queue groups) and JetStream (streams, durable consumers, acks)

go
import "go.digitalxero.dev/mq-nats/v2"

Index

funcNewMessageBuilder

go
func NewMessageBuilder() types.MessageBuilder

NewMessageBuilder creates a new NATS message builder. The builder provides a fluent interface for constructing NATS messages with all supported properties and attributes.

Example:

go
msg := nats.NewMessageBuilder().
    WithBody([]byte(`{"sensor": "temp-01", "value": 23.5}`)).
    WithContentType("application/json").
    WithCorrelationId("reading-456").
    WithHeaders(map[string]interface{}{
        "location": "warehouse-1",
        "timestamp": time.Now().Unix(),
    }).
    WithReplyTo("sensor.responses"). // For request-reply pattern
    Build()

funcNewNatsConsumer

go
func NewNatsConsumer(config *viper.Viper) (types.Consumer, error)

NewNatsConsumer creates a new NATS/JetStream consumer with the provided configuration. This function is typically called by mqutils.NewConsumer when it detects a NATS URL.

keydefaultdescription
urlrequiredNATS server URLs e.g., “nats://localhost:4222”
subjectrequiredSubject to consume from
handler“natsLogger”Name of registered handler function
queue_groupoptionalQueue group for load balancing
use_jetstreamfalseEnable JetStream mode
stream_nameJetStream stream name (required when use_jetstream is true)
consumer_name“mqutils-consumer”JetStream consumer name
durablefalseCreate a durable JetStream consumer
max_deliver50Max JetStream delivery attempts before dropping
ack_wait30000JetStream ack wait in ms
auto_reconnectfalseReconnect the consumer loop on connection loss
skip_verifyfalseSkip TLS certificate verification
max_reconnects60Max reconnection attempts
reconnect_wait2000Wait between reconnects in ms
reconnect_buf_size0 = 8MB library default; -1 disablesClient-side buffer (bytes) for publishes while disconnected
fetch_batch_size10JetStream pull-fetch batch size
fetch_max_wait_ms1000JetStream pull-fetch max wait in ms
message_channel_buffer10Buffer size for message channel
batch_size5Number of messages per batch
batch_timeout100msBatch collection timeout as a duration
enable_batch_processingfalseEnable batch message processing
max_concurrent_handlerseffective message_channel_buffer; must be positiveMaximum in-flight handlers or batches

The canonical keys destination, max_retries, and consumer_group are accepted as aliases for subject, max_deliver, and queue_group respectively; the native key wins when both are set.

Returns an error if configuration validation fails or transport creation fails.

funcNewNatsProducer

go
func NewNatsProducer(config *viper.Viper) (types.Producer, error)

NewNatsProducer creates a new NATS/JetStream producer with the provided configuration. This function is typically called by mqutils.NewProducer when it detects a NATS URL.

keydefaultdescription
urlrequiredNATS server URLs e.g., “nats://localhost:4222”
use_jetstreamfalseEnable JetStream mode for persistence
stream_nameJetStream stream to declare on Start (required when use_jetstream is true)
skip_verifyfalseSkip TLS certificate verification
max_reconnects60Max reconnection attempts
reconnect_wait2000Wait between reconnects in ms
reconnect_buf_size0 = 8MB library default; -1 disables bufferingClient-side buffer (bytes) that queues outgoing publishes while the connection is down; beyond it publishes fail with “nats: outbound buffer limit exceeded”
request_timeout5000Timeout for request-reply in ms

The canonical key destination is accepted as an alias for stream_name; the native key wins when both are set.

Returns an error if configuration validation fails or transport creation fails. The producer must be started with Start() before publishing messages.

funcNewNatsTransport

go
func NewNatsTransport() types.Transport

NewNatsTransport creates a new NATS transport implementation with default options. The transport provides low-level NATS operations including connection management, publishing, and subscription handling for both Core NATS and JetStream.

The transport manages:

  • NATS connections with automatic reconnection
  • JetStream context for persistent messaging
  • Subscription lifecycle management
  • Authentication (credentials, TLS)
  • Health monitoring

The transport supports both standard NATS (at-most-once delivery) and JetStream (at-least-once with persistence) messaging patterns.

Generated by gomarkdoc

move open/ opens search anywhere