NATS.js
Official Node.js client for NATS core messaging, JetStream persistence, queues and request-reply.
Official Documentation ↗nats v2.x / NATS Server 2.10+ / Node 18 LTS. Check the official docs for the latest API surface.Installation
npm install natspnpm add natsyarn add natsMethods
No methods match this filter.
Overview
NATS.js connects Node applications to NATS for lightweight pub-sub, request-reply and, through JetStream, durable streams with acknowledgements and replay.
Core Methods
connect returns a connection. publish and subscribe cover fire-and-forget messaging, request implements request-reply, and jetstream opens a context for persistent publish and consume operations.
Usage Notes
Use core NATS for high-frequency, loss-tolerant signals and JetStream where messages must survive consumer outages. Use queue groups to load-balance subscribers.
Common Pitfalls
Core subscriptions receive only messages published after subscribing, so late subscribers miss history unless JetStream is used. Forgetting to drain the connection on shutdown can drop in-flight messages.
FAQ
- What is the difference between core NATS and JetStream?
- Core NATS is at-most-once and in-memory, delivering only to current subscribers. JetStream adds persistence, replay, acknowledgements and consumer groups for durable delivery.
- How does request-reply work?
- Publish to a subject and await nc.request(subject, data, { timeout }). A responder subscribes and replies, and the client correlates the reply automatically.
- Are subjects persistent?
- Subjects are just routing names and have no storage. Durability comes from JetStream streams that persist messages for a subject space.
Report a problem with this API docOpens a pre-filled issue on GitHub.