Publish, subscribe, and request/reply over NATS from your pack’s frontend.
useNats is your pack’s async transport. It wraps the platform’s NATS bridge so your frontend can talk to your service, react to events from other packs, or broadcast messages across the org.
When you need an answer, use request. This pattern is how pack-terminal spawns a PTY session — the frontend requests, the service responds with the new session id.
publish never throws. If the bridge is offline, the frame is dropped.
subscribe never throws. Callbacks that throw are isolated — other subscribers still fire.
request rejects with an Error when it times out. Always wrap in try/catch or .catch().
Connection drops: read connected and guard side effects on it. Re-subscribing on reconnect is automatic — the bridge replays your active subscriptions.
* matches exactly one token: khal.*.terminal.pty.spawn
> matches one or more tokens, and must be last: khal.<orgId>.terminal.pty.>
Never subscribe to khal.internal.* or khal.catalog.*. Those subjects are platform plumbing — they’re not part of the SDK contract, they change without notice, and the bridge will reject subscriptions that aren’t yours. Scope everything under your pack’s id.
Current user, org, role — the auth contract your pack receives.
useService
Call into your pack’s own backend service.
🤖 Agent paste prompt
TASK: Use the Khal OS docs page `sdk/useNats.mdx` (useNats) to guide an FDE through the workflow, run only safe verification commands first, and return evidence with blockers.CONTEXT: You are helping a KHAL Forward Deployed Engineer. Read this page, prefer current command output over memory, and keep secrets/private URLs out of public artifacts.SAFE FIRST COMMANDS: Check tool versions, auth status, target identity, git branch/ref, and dry-run output before any mutation.EVIDENCE: Return command, exit status, sanitized output, and the next safe action. If credentials, SSH, Gitea, KHAW, or target access are missing, produce the exact access request instead of guessing.