vael:nats
Async NATS messaging and JetStream client (vael:nats).
Available in the vjs (vaeljs) runtime only when built with the opt-in
vael-nats (or vael-messaging) Cargo feature; absent in default builds
and in vjs-lite.
import ... from "vael:nats"
vael-nats / vael-messaging (opt-in in vaeljs-core, disabled by default; absent in vaeljs-lite-core)Example
Section titled “Example”import { connect } from "vael:nats";
const nc = await connect({ host: "127.0.0.1", port: 4222 });try { await nc.publish("jobs.created", JSON.stringify({ id: 1 })); await nc.flush();} finally { await nc.close();}NatsConnectOptionsvjs onlyv0.0.0-alpha.2connect(options).NatsPublishOptionsvjs onlyv0.0.0-alpha.2NatsClient.publish() and NatsMessage.respond().NatsRequestOptionsvjs onlyv0.0.0-alpha.2NatsClient.request().NatsSubscribeOptionsvjs onlyv0.0.0-alpha.2NatsClient.subscribe().NatsMessagevjs onlyv0.0.0-alpha.2NatsSubscriptionvjs onlyv0.0.0-alpha.2NatsClient.subscribe().JetStreamPublishOptionsvjs onlyv0.0.0-alpha.2JetStreamClient.publish().JetStreamFetchOptionsvjs onlyv0.0.0-alpha.2JetStreamClient.fetch().JetStreamAckvjs onlyv0.0.0-alpha.2JetStreamClient.publish().JetStreamClientvjs onlyv0.0.0-alpha.2NatsClient.jetstream().NatsClientvjs onlyv0.0.0-alpha.2connect().Functions
Section titled “Functions”connect
Section titled “connect”export function connect(options: NatsConnectOptions): Promise<NatsClient>;
Connect asynchronously to a NATS server (vael-nats feature required).
| Name | Type | Description |
|---|---|---|
options | NatsConnectOptions | NATS connection options. |
Promise<NatsClient> — Promise resolving to a connected NatsClient.TypeError— on invalid argument types.NatsError— on connection, authentication, TLS, or timeout failures.
Classes
Section titled “Classes”NatsError
Section titled “NatsError”export class NatsError extends Error {
override name: "NatsError";
code: NatsErrorCode;
constructor(code: NatsErrorCode, message: string);
}
Redacted NATS client error carrying a stable NatsErrorCode.
constructor(code: NatsErrorCode, message: string);
Construct a NatsError.
| Name | Type | Description |
|---|---|---|
code | NatsErrorCode | Machine-readable NatsErrorCode. |
message | string | Human-readable redacted error description. |
| Property | Type | Description | Since |
|---|---|---|---|
name | "NatsError" | Error name ("NatsError"). | v0.0.0-alpha.2 |
code | NatsErrorCode | Machine-readable NATS error code. | v0.0.0-alpha.2 |
Interfaces
Section titled “Interfaces”NatsConnectOptions
Section titled “NatsConnectOptions”export interface NatsConnectOptions {
host: string;
port?: number;
user?: string;
pass?: string;
token?: string;
name?: string;
tls?: boolean;
connectTimeoutMs?: number;
requestTimeoutMs?: number;
}
Connection configuration accepted by connect(options).
| Property | Type | Description | Since |
|---|---|---|---|
host | string | Server hostname or IP address (required). | v0.0.0-alpha.2 |
port optional | number | Server TCP port (1..=65535, default 4222). | v0.0.0-alpha.2 |
user optional | string | Optional username for CONNECT authentication. | v0.0.0-alpha.2 |
pass optional | string | Optional password for CONNECT authentication. | v0.0.0-alpha.2 |
token optional | string | Optional bearer auth token for CONNECT authentication. | v0.0.0-alpha.2 |
name optional | string | Optional client name sent in CONNECT. | v0.0.0-alpha.2 |
tls optional | boolean | Enable fail-closed rustls TLS upgrade (default false). | v0.0.0-alpha.2 |
connectTimeoutMs optional | number | TCP/TLS connect timeout in milliseconds (1..=300000). | v0.0.0-alpha.2 |
requestTimeoutMs optional | number | Default timeout in milliseconds for request() and flush() (1..=300000). | v0.0.0-alpha.2 |
NatsPublishOptions
Section titled “NatsPublishOptions”export interface NatsPublishOptions {
reply?: string;
headers?: Record<string, string | readonly string[]>;
}
Options accepted by NatsClient.publish() and NatsMessage.respond().
| Property | Type | Description | Since |
|---|---|---|---|
reply optional | string | Optional reply-to subject for request/reply routing. | v0.0.0-alpha.2 |
headers optional | Record<string, string | readonly string[]> | Optional NATS message headers (HPUB / HMSG). | v0.0.0-alpha.2 |
NatsRequestOptions
Section titled “NatsRequestOptions”export interface NatsRequestOptions {
headers?: Record<string, string | readonly string[]>;
timeoutMs?: number;
}
Options accepted by NatsClient.request().
| Property | Type | Description | Since |
|---|---|---|---|
headers optional | Record<string, string | readonly string[]> | Optional NATS message headers. | v0.0.0-alpha.2 |
timeoutMs optional | number | Request deadline in milliseconds (1..=300000). | v0.0.0-alpha.2 |
NatsSubscribeOptions
Section titled “NatsSubscribeOptions”export interface NatsSubscribeOptions {
queue?: string;
max?: number;
}
Options accepted by NatsClient.subscribe().
| Property | Type | Description | Since |
|---|---|---|---|
queue optional | string | Optional queue group name for load-balanced delivery. | v0.0.0-alpha.2 |
max optional | number | Automatically unsubscribe after receiving at most max messages. | v0.0.0-alpha.2 |
NatsMessage
Section titled “NatsMessage”export interface NatsMessage {
readonly subject: string;
readonly reply?: string;
readonly data: Uint8Array;
readonly headers?: Record<string, readonly string[]>;
readonly status?: number;
string(): string;
json<T = unknown>(): T;
respond(payload?: string | Uint8Array, options?: NatsPublishOptions): Promise<void>;
ack(): Promise<void>;
nak(): Promise<void>;
term(): Promise<void>;
inProgress(): Promise<void>;
}
Message received from a NATS subscription, request reply, or JetStream pull.
| Property | Type | Description | Since |
|---|---|---|---|
subject readonly | string | Subject on which the message was delivered. | v0.0.0-alpha.2 |
reply readonly optional | string | Optional reply subject for respond() / JetStream acknowledgments. | v0.0.0-alpha.2 |
data readonly | Uint8Array | Raw payload bytes. | v0.0.0-alpha.2 |
headers readonly optional | Record<string, readonly string[]> | Parsed multi-value NATS headers, if present. | v0.0.0-alpha.2 |
status readonly optional | number | Optional NATS status code (e.g., 503 for no-responders). | v0.0.0-alpha.2 |
NatsMessage.string
Section titled “NatsMessage.string”string(): string;
Decode data as UTF-8 text.
stringNatsMessage.json
Section titled “NatsMessage.json”json<T = unknown>(): T;
Decode data as UTF-8 and parse as JSON.
TNatsMessage.respond
Section titled “NatsMessage.respond”respond(payload?: string | Uint8Array, options?: NatsPublishOptions): Promise<void>;
Publish a reply to msg.reply. Throws NatsError if no reply is present.
| Name | Type | Description |
|---|---|---|
payload optional | string | Uint8Array | — |
options optional | NatsPublishOptions | — |
Promise<void>NatsMessage.ack
Section titled “NatsMessage.ack”ack(): Promise<void>;
Send a JetStream positive acknowledgment (+ACK) if reply is present.
Promise<void>NatsMessage.nak
Section titled “NatsMessage.nak”nak(): Promise<void>;
Send a JetStream negative acknowledgment (-NAK) if reply is present.
Promise<void>NatsMessage.term
Section titled “NatsMessage.term”term(): Promise<void>;
Send a JetStream terminal acknowledgment (+TERM) if reply is present.
Promise<void>NatsMessage.inProgress
Section titled “NatsMessage.inProgress”inProgress(): Promise<void>;
Notify JetStream that work is still in progress (+WPI) to reset ack wait if reply is present.
Promise<void>NatsSubscription
Section titled “NatsSubscription”export interface NatsSubscription {
next(): Promise<NatsMessage | null>;
unsubscribe(): Promise<void>;
[Symbol.asyncIterator](): AsyncIterableIterator<NatsMessage>;
}
Active NATS subscription returned by NatsClient.subscribe().
NatsSubscription.next
Section titled “NatsSubscription.next”next(): Promise<NatsMessage | null>;
Await the next delivered message, or null once unsubscribed or exhausted.
Promise<NatsMessage | null>NatsSubscription.unsubscribe
Section titled “NatsSubscription.unsubscribe”unsubscribe(): Promise<void>;
Unsubscribe and stop receiving further messages.
Promise<void>NatsSubscription.[Symbol.asyncIterator]
Section titled “NatsSubscription.[Symbol.asyncIterator]”[Symbol.asyncIterator](): AsyncIterableIterator<NatsMessage>;
Iterate asynchronously over incoming messages (for await (const msg of sub)).
JetStreamPublishOptions
Section titled “JetStreamPublishOptions”export interface JetStreamPublishOptions {
msgId?: string;
timeoutMs?: number;
}
Options accepted by JetStreamClient.publish().
| Property | Type | Description | Since |
|---|---|---|---|
msgId optional | string | Optional deduplication message ID (Nats-Msg-Id header). | v0.0.0-alpha.2 |
timeoutMs optional | number | Publish acknowledgment deadline in milliseconds (1..=300000). | v0.0.0-alpha.2 |
JetStreamFetchOptions
Section titled “JetStreamFetchOptions”export interface JetStreamFetchOptions {
maxMessages?: number;
expiresMs?: number;
}
Options accepted by JetStreamClient.fetch().
| Property | Type | Description | Since |
|---|---|---|---|
maxMessages optional | number | Maximum number of messages to pull (1..=1024). | v0.0.0-alpha.2 |
expiresMs optional | number | Pull expiration timeout in milliseconds (1..=300000). | v0.0.0-alpha.2 |
JetStreamAck
Section titled “JetStreamAck”export interface JetStreamAck {
readonly stream: string;
readonly seq: number;
readonly duplicate: boolean;
}
Server acknowledgment returned by JetStreamClient.publish().
| Property | Type | Description | Since |
|---|---|---|---|
stream readonly | string | Stream that persisted the message. | v0.0.0-alpha.2 |
seq readonly | number | Assigned stream sequence number. | v0.0.0-alpha.2 |
duplicate readonly | boolean | true if the server deduplicated this message by msgId. | v0.0.0-alpha.2 |
JetStreamClient
Section titled “JetStreamClient”export interface JetStreamClient {
publish(
subject: string,
payload?: string | Uint8Array,
options?: JetStreamPublishOptions,
): Promise<JetStreamAck>;
streamInfo(stream: string): Promise<Record<string, unknown>>;
createStream(config: Record<string, unknown>): Promise<Record<string, unknown>>;
deleteStream(stream: string): Promise<Record<string, unknown>>;
createConsumer(
stream: string,
config: Record<string, unknown>,
): Promise<Record<string, unknown>>;
fetch(
stream: string,
consumer: string,
options?: JetStreamFetchOptions,
): Promise<NatsMessage[]>;
}
JetStream client facade returned by NatsClient.jetstream().
JetStreamClient.publish
Section titled “JetStreamClient.publish”publish(
subject: string,
payload?: string | Uint8Array,
options?: JetStreamPublishOptions,
): Promise<JetStreamAck>;
Publish a message to a JetStream subject and await server acknowledgment.
| Name | Type | Description |
|---|---|---|
subject | string | — |
payload optional | string | Uint8Array | — |
options optional | JetStreamPublishOptions | — |
JetStreamClient.streamInfo
Section titled “JetStreamClient.streamInfo”streamInfo(stream: string): Promise<Record<string, unknown>>;
Retrieve JetStream stream metadata via $JS.API.STREAM.INFO.
| Name | Type | Description |
|---|---|---|
stream | string | — |
JetStreamClient.createStream
Section titled “JetStreamClient.createStream”createStream(config: Record<string, unknown>): Promise<Record<string, unknown>>;
Create or update a JetStream stream via $JS.API.STREAM.CREATE.
| Name | Type | Description |
|---|---|---|
config | Record<string, unknown> | — |
JetStreamClient.deleteStream
Section titled “JetStreamClient.deleteStream”deleteStream(stream: string): Promise<Record<string, unknown>>;
Delete a JetStream stream via $JS.API.STREAM.DELETE.
| Name | Type | Description |
|---|---|---|
stream | string | — |
JetStreamClient.createConsumer
Section titled “JetStreamClient.createConsumer”createConsumer(
stream: string,
config: Record<string, unknown>,
): Promise<Record<string, unknown>>;
Create or update a JetStream durable/ephemeral consumer.
| Name | Type | Description |
|---|---|---|
stream | string | — |
config | Record<string, unknown> | — |
JetStreamClient.fetch
Section titled “JetStreamClient.fetch”fetch(
stream: string,
consumer: string,
options?: JetStreamFetchOptions,
): Promise<NatsMessage[]>;
Pull up to options.maxMessages messages from a JetStream pull consumer.
| Name | Type | Description |
|---|---|---|
stream | string | — |
consumer | string | — |
options optional | JetStreamFetchOptions | — |
NatsClient
Section titled “NatsClient”export interface NatsClient {
publish(
subject: string,
payload?: string | Uint8Array,
options?: NatsPublishOptions,
): Promise<void>;
request(
subject: string,
payload?: string | Uint8Array,
options?: NatsRequestOptions,
): Promise<NatsMessage>;
subscribe(subject: string, options?: NatsSubscribeOptions): NatsSubscription;
flush(): Promise<void>;
jetstream(): JetStreamClient;
close(): Promise<void>;
[Symbol.asyncDispose](): Promise<void>;
}
Connected async NATS client handle returned by connect().
NatsClient.publish
Section titled “NatsClient.publish”publish(
subject: string,
payload?: string | Uint8Array,
options?: NatsPublishOptions,
): Promise<void>;
Publish payload to subject.
| Name | Type | Description |
|---|---|---|
subject | string | — |
payload optional | string | Uint8Array | — |
options optional | NatsPublishOptions | — |
Promise<void>NatsClient.request
Section titled “NatsClient.request”request(
subject: string,
payload?: string | Uint8Array,
options?: NatsRequestOptions,
): Promise<NatsMessage>;
Send a request to subject and await a single NatsMessage reply.
| Name | Type | Description |
|---|---|---|
subject | string | — |
payload optional | string | Uint8Array | — |
options optional | NatsRequestOptions | — |
NatsClient.subscribe
Section titled “NatsClient.subscribe”subscribe(subject: string, options?: NatsSubscribeOptions): NatsSubscription;
Synchronously create a subscription to subject (supporting * and > wildcards).
| Name | Type | Description |
|---|---|---|
subject | string | — |
options optional | NatsSubscribeOptions | — |
NatsClient.flush
Section titled “NatsClient.flush”flush(): Promise<void>;
Send PING and await PONG round-trip to flush outbound frames.
Promise<void>NatsClient.jetstream
Section titled “NatsClient.jetstream”jetstream(): JetStreamClient;
Return a JetStreamClient bound to this NATS connection.
NatsClient.close
Section titled “NatsClient.close”close(): Promise<void>;
Close the NATS connection immediately. Idempotent.
Promise<void>NatsClient.[Symbol.asyncDispose]
Section titled “NatsClient.[Symbol.asyncDispose]”[Symbol.asyncDispose](): Promise<void>;
Close the connection asynchronously when disposed via await using.
Promise<void>Type Aliases
Section titled “Type Aliases”NatsErrorCode
Section titled “NatsErrorCode”export type NatsErrorCode =
| "VAEL_NATS_CONFIG"
| "VAEL_NATS_CONNECT"
| "VAEL_NATS_TLS"
| "VAEL_NATS_AUTH"
| "VAEL_NATS_TIMEOUT"
| "VAEL_NATS_PROTOCOL"
| "VAEL_NATS_SERVER"
| "VAEL_NATS_LIMIT"
| "VAEL_NATS_CLOSED"
| "VAEL_NATS_IO"
| "VAEL_NATS_NOT_FOUND";
Stable screaming-snake-case error codes reported on NatsError.code.
