nats.go
Go client for NATS core messaging, request-reply and JetStream persistence.
Official Documentation ↗nats.go v1.37 / NATS Server 2.10+ / Go 1.20+. Check the official docs for the latest API surface.Installation
go get github.com/nats-io/nats.goMethods
No methods match this filter.
Overview
nats.go connects Go applications to NATS for lightweight messaging, request-reply and durable JetStream streams. It handles reconnection, buffering and asynchronous delivery.
Core Methods
Connect with options, publish and subscribe on subjects, and use NC.Request for request-reply. nc.JetStream opens a context for persistent publish and consume operations.
Usage Notes
Use core NATS for high-rate, loss-tolerant signals and JetStream for durable messages. Use queue groups to load-balance subscribers and handle the closed callback for permanent disconnects.
Common Pitfalls
Core subscribers miss messages published before they subscribed. Unbounded outbound buffering during outages can exhaust memory. Forgetting to drain on shutdown drops in-flight messages.
Security notes
Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/nats-io/nats.go@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:03.416Z.
- New Authentication (Nkeys and User Credentials) This requires server with version >= 2.0.0 NATS servers have a new security and authentication mechanism to authenticate with user credentials and Nkeys. (https://cdn.jsdelivr.net/gh/nats-io/nats.go@latest/README.md)
FAQ
- Does core NATS persist messages?
- No. Core NATS is at-most-once and delivers only to current subscribers. JetStream adds streams, persistence, replay and acknowledgements.
- How does request-reply work?
- nc.Request publishes and waits for a reply on an inbox subject, returning the response message or a timeout error. The responder replies on the provided reply subject.
- How do I handle reconnection?
- nats.Connect reconnects by default with buffering of outbound messages. Configure max reconnect attempts, reconnect wait and a closed handler so the application can react to permanent loss.
Report a problem with this API docOpens a pre-filled issue on GitHub.