nats-messaging
Installation
Review the source and dependencies with a prompt. A package is available to download.
Install with an AI assistant
Copy the prompt so your assistant can review the pinned revision and dependencies first.
View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "nats-messaging" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-44305814c61d728d1d6cbea5bdc8894a27b547186fda7451a4bcd4f7e49aff91. Verify the upstream source and version against https://github.com/TerminalSkills/skills before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
github.com
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
For building distributed messaging systems with NATS, supporting pub/sub, request/reply, JetStream persistent messaging, and key-value storage. Suitable when users ask about setting up message queues, pub/sub systems, event-driven architectures, NATS messaging, microservice communication, or simpler alternatives to Kafka/RabbitMQ. Covers core NATS, JetStream, KV store, and object store scenarios.
Introduction source: third_party · TerminalSkills · 1.0.0 Reference
SkillPi interpretation
- Audience
- Microservice-to-microservice communication (events, commands, queries) Real-time data streaming with persistence and replay Distributed key-value store without running Redis Request/reply patterns (synchronous messaging over async transport) Replacing Kafka/RabbitMQ in small-to-medium deployments
- Software, accounts and paid services
- ```bash
- Testing
- Not tested
Browse files
Frontmatter
name: nats-messaging
description: >-
Build distributed messaging systems with NATS — pub/sub, request/reply,
JetStream persistent messaging, and key-value store. Use when someone asks
to "set up message queue", "pub/sub system", "event-driven architecture",
"NATS messaging", "distributed messaging", "microservice communication",
"message broker", or "replace Kafka/RabbitMQ with something simpler".
Covers core NATS, JetStream, KV store, and object store.
license: Apache-2.0
compatibility: "Node.js 18+. NATS server (nats-server or Synadia Cloud)."
metadata:
author: terminal-skills
version: "1.0.0"
category: development
tags: ["messaging", "pub-sub", "nats", "queue", "distributed"]NATS Messaging
Overview
NATS is a lightweight, high-performance messaging system for distributed applications. Simpler than Kafka, faster than RabbitMQ, with built-in persistence (JetStream), key-value store, and object store. Single binary, zero dependencies, runs anywhere.
When to Use
- Microservice-to-microservice communication (events, commands, queries)
- Real-time data streaming with persistence and replay
- Distributed key-value store without running Redis
- Request/reply patterns (synchronous messaging over async transport)
- Replacing Kafka/RabbitMQ in small-to-medium deployments
Instructions
Setup
# Install NATS server
docker run -d --name nats -p 4222:4222 -p 8222:8222 nats:latest -js
# Install client
npm install nats
Core Pub/Sub
// pub-sub.ts — Basic publish/subscribe messaging
import { connect, StringCodec } from "nats";
const nc = await connect({ servers: "localhost:4222" });
const sc = StringCodec();
// Subscribe
const sub = nc.subscribe("orders.created");
(async () => {
for await (const msg of sub) {
const order = JSON.parse(sc.decode(msg.data));
console.log(`New order: ${order.id} — $${order.total}`);
}
})();
// Publish
nc.publish("orders.created", sc.encode(JSON.stringify({
id: "ord_123",
total: 99.99,
items: ["widget-a", "widget-b"],
})));
JetStream (Persistent Messaging)
// jetstream.ts — Durable streams with replay and acknowledgment
import { connect, StringCodec, AckPolicy, DeliverPolicy } from "nats";
const nc = await connect({ servers: "localhost:4222" });
const js = nc.jetstream();
const jsm = await nc.jetstreamManager();
const sc = StringCodec();
// Create a stream (like a Kafka topic)
await jsm.streams.add({
name: "ORDERS",
subjects: ["orders.>"], // Capture all order events
retention: "limits", // Keep messages until limits hit
max_msgs: 1_000_000,
max_age: 7 * 24 * 60 * 60 * 1e9, // 7 days in nanoseconds
});
// Publish to stream
await js.publish("orders.created", sc.encode(JSON.stringify({
id: "ord_456", total: 149.99,
})));
// Durable consumer (survives restarts)
const consumer = await jsm.consumers.add("ORDERS", {
durable_name: "order-processor",
ack_policy: AckPolicy.Explicit,
deliver_policy: DeliverPolicy.All, // Replay from beginning
});
// Process messages
const sub = await js.consumers.get("ORDERS", "order-processor");
const messages = await sub.consume();
for await (const msg of messages) {
const order = JSON.parse(sc.decode(msg.data));
console.log(`Processing: ${order.id}`);
msg.ack(); // Acknowledge — won't be redelivered
}
Request/Reply
// request-reply.ts — Synchronous messaging pattern
import { connect, StringCodec } from "nats";
const nc = await connect({ servers: "localhost:4222" });
const sc = StringCodec();
// Service (responder)
nc.subscribe("users.get", {
callback: async (err, msg) => {
const { id } = JSON.parse(sc.decode(msg.data));
const user = await db.user.findUnique({ where: { id } });
msg.respond(sc.encode(JSON.stringify(user)));
},
});
// Client (requester) — waits for response
const response = await nc.request(
"users.get",
sc.encode(JSON.stringify({ id: "user_123" })),
{ timeout: 5000 } // 5 second timeout
);
const user = JSON.parse(sc.decode(response.data));
Key-Value Store
// kv.ts — Distributed key-value store (replaces Redis for simple cases)
import { connect } from "nats";
const nc = await connect({ servers: "localhost:4222" });
const js = nc.jetstream();
// Create KV bucket
const kv = await js.views.kv("sessions");
// Set
await kv.put("user:123", JSON.stringify({ token: "abc", expiresAt: Date.now() + 3600000 }));
// Get
const entry = await kv.get("user:123");
const session = JSON.parse(entry?.string() || "null");
// Watch for changes (real-time)
const watch = await kv.watch();
for await (const entry of watch) {
console.log(`${entry.key} changed: ${entry.string()}`);
}
// Delete
await kv.delete("user:123");
Examples
Example 1: Event-driven microservice architecture
User prompt: "Set up event-driven communication between 3 microservices: orders, payments, and notifications."
The agent will create a JetStream stream for each domain, publish domain events (order.created, payment.completed), and set up durable consumers in each service.
Example 2: Replace Redis with NATS KV
User prompt: "I need a key-value store for session data but don't want to run Redis."
The agent will set up NATS KV bucket for sessions with TTL, get/set/delete operations, and watch for real-time session changes.
Guidelines
- Core NATS for fire-and-forget — fast pub/sub, no persistence
- JetStream for durable messaging — when messages must not be lost
- Explicit ack for reliability — acknowledge after processing, not before
- Subject hierarchy with
.—orders.created,orders.shipped, subscribe toorders.> - KV replaces Redis for simple cases — session storage, config, feature flags
- Single binary — NATS server is 15MB, runs anywhere, no JVM
- Cluster for HA — 3-node cluster for production resilience
- Consumer groups — multiple instances of the same consumer share the workload
- Max 1MB per message — use Object Store for larger payloads
Current listing version
1.0.0
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Highlights
- NATS 提供轻量级、高性能的分布式消息系统,适用于微服务通信、事件驱动架构等高频场景
- 支持 JetStream 持久化、KV store、request/reply 等多种模式,可替代 Kafka/RabbitMQ/Redis
Highlights
- 触发词覆盖全面,包括 message queue、pub/sub、event-driven、NATS messaging、distributed messaging 等关键场景
- 边界清晰,明确区分核心 NATS 与 JetStream/KV 的使用场景
Highlights
- 结构清晰,分 Setup、Core Pub/Sub、JetStream、Request/Reply、KV Store 等模块
- 每个模块提供完整可运行的 TypeScript 代码示例,含详细注释和配置说明
Highlights
- 工具来源明确(Docker 镜像、npm 包),参数配置正确
- 覆盖主要异常处理(如 request timeout、ack 机制),JetStream 提供持久化和 replay 保障
The arithmetic mean of the first four original scores, rounded to a whole number.
Static text checks
100 / 100 · skillpi-20261123-no-r3-v1
No current rules matched the text.
This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.
Permissions and checks before use
Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.
-
Install or fetch external dependencies
Use official sources and pin versions. Check install scripts in an isolated environment. This document check does not cover code downloaded later.
Mentioned in main document · lines 42
Security rating and access scope
safe · Static evidence review; not a runtime safety guarantee.
```bash
Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.
User discussion
No verified reviews yet.