nats-messaging
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "nats-messaging":https://skillpi.com/zh/skills/deepskill-44305814c61d728d1d6cbea5bdc8894a27b547186fda7451a4bcd4f7e49aff91。安装前核对上游来源和版本:https://github.com/TerminalSkills/skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
用于使用 NATS 构建分布式消息系统,支持发布/订阅、请求/响应、JetStream 持久化消息及键值存储。适合提出“搭建消息队列”、“发布/订阅系统”、“事件驱动架构”、“NATS 消息传递”、“微服务通信”或寻求替代 Kafka/RabbitMQ 的简单方案的需求。涵盖核心 NATS、JetStream、KV 存储和对象存储相关场景。
介绍来源: third_party · TerminalSkills · 1.0.0 参考来源
SkillPi 解读
- 适合谁
- 翻译待完成,请参阅来源文档。
- 软件、账号及付费要求
- 贝什
- 实测状态
- 未测试
文件浏览
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
当前收录版本
1.0.0
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- NATS 提供轻量级、高性能的分布式消息系统,适用于微服务通信、事件驱动架构等高频场景
- 支持 JetStream 持久化、KV store、request/reply 等多种模式,可替代 Kafka/RabbitMQ/Redis
亮点
- 触发词覆盖全面,包括 message queue、pub/sub、event-driven、NATS messaging、distributed messaging 等关键场景
- 边界清晰,明确区分核心 NATS 与 JetStream/KV 的使用场景
亮点
- 结构清晰,分 Setup、Core Pub/Sub、JetStream、Request/Reply、KV Store 等模块
- 每个模块提供完整可运行的 TypeScript 代码示例,含详细注释和配置说明
亮点
- 工具来源明确(Docker 镜像、npm 包),参数配置正确
- 覆盖主要异常处理(如 request timeout、ack 机制),JetStream 提供持久化和 replay 保障
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
-
安装或获取外部依赖
使用官方来源并锁定版本,在隔离环境检查安装脚本。当前文档检查不覆盖后续下载的代码。
主文档提及 · 第 42 行
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
贝什
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。