edgeport - v1.0.3
    Preparing search index...

    Interface NatsConnection

    A live NATS connection over a single socket.

    Obtain one from connect. A background pump reads the socket and routes messages, so publishing, subscribing, and requesting can all be in flight at once. It is an AsyncDisposable, so await using closes it cleanly.

    1.0.0

    interface NatsConnection {
        "[asyncDispose]"(): PromiseLike<void>;
        close(): Promise<void>;
        jetstream(): JetStreamManager;
        publish(
            subject: string,
            data?: string | Uint8Array<ArrayBufferLike>,
            opts?: { reply?: string },
        ): Promise<void>;
        publishJson(
            subject: string,
            value: unknown,
            opts?: { reply?: string },
        ): Promise<void>;
        request(
            subject: string,
            data?: string | Uint8Array<ArrayBufferLike>,
            opts?: { timeoutMs?: number },
        ): Promise<NatsMessage>;
        requestJson<T = unknown>(
            subject: string,
            value: unknown,
            opts?: { timeoutMs?: number },
        ): Promise<T>;
        subscribe(subject: string, opts?: { queue?: string }): NatsSubscription;
        subscribeJson<T = unknown>(
            subject: string,
            opts?: { queue?: string },
        ): AsyncIterable<NatsJsonMessage<T>, any, any> & AsyncDisposable;
    }

    Hierarchy

    • AsyncDisposable
      • NatsConnection
    Index
    • Returns PromiseLike<void>

    • Closes the connection and ends every subscription.

      Returns Promise<void>

      Resolves once the socket is closed.

    • Returns a JetStreamManager bound to this connection for durable streaming.

      JetStream layers a JSON request-reply protocol over the same socket, so the manager simply reuses this connection's publish/request. The server must have JetStream enabled (-js).

      Returns JetStreamManager

      A JetStream context for ensuring streams, publishing with PubAcks, and durable pulls.

    • Publishes a message to a subject.

      Parameters

      • subject: string

        The subject to publish to.

      • Optionaldata: string | Uint8Array<ArrayBufferLike>

        The payload (string is UTF-8 encoded); empty if omitted.

      • Optionalopts: { reply?: string }

        Optional reply subject for request-reply.

      Returns Promise<void>

      Resolves once the PUB frame is written.

      If the connection is closed.

    • Publishes a value as JSON to a subject.

      The value is JSON.stringify-ed and encoded as UTF-8, then published with publish.

      Parameters

      • subject: string

        The subject to publish to.

      • value: unknown

        The value to serialize and publish.

      • Optionalopts: { reply?: string }

        Optional reply subject for request-reply.

      Returns Promise<void>

      Resolves once the PUB frame is written.

      If the connection is closed.

      1.0.2

    • Sends a request and waits for the first reply on a private inbox.

      Parameters

      • subject: string

        The subject to send the request to.

      • Optionaldata: string | Uint8Array<ArrayBufferLike>

        The request payload (string is UTF-8 encoded).

      • Optionalopts: { timeoutMs?: number }

        Optional per-request timeout (defaults to 2000ms).

      Returns Promise<NatsMessage>

      The first reply message.

      If no reply arrives before the deadline.

      If the connection is closed.

    • Sends a JSON request and parses the reply payload as JSON.

      Serializes value with JSON.stringify, sends it via request, then UTF-8 decodes and JSON.parses the reply.

      Type Parameters

      • T = unknown

        The expected shape of the decoded reply.

      Parameters

      • subject: string

        The subject to send the request to.

      • value: unknown

        The request value to serialize.

      • Optionalopts: { timeoutMs?: number }

        Optional per-request timeout (defaults to 2000ms).

      Returns Promise<T>

      The parsed reply value.

      If no reply arrives before the deadline.

      If the connection is closed.

      If the reply payload is not valid JSON.

      1.0.2

    • Subscribes to a subject, optionally as part of a queue group.

      Parameters

      • subject: string

        The subject (may contain * / > wildcards).

      • Optionalopts: { queue?: string }

        Optional queue group name for load-balanced delivery.

      Returns NatsSubscription

      A subscription whose async iterator yields matching messages.

    • Subscribes to a subject and yields each message with its payload parsed from JSON.

      A thin JSON layer over subscribe: each delivered message's payload is UTF-8 decoded and JSON.parse-d. Iterate the result with for await. It is an AsyncDisposable, so await using unsubscribes automatically.

      Type Parameters

      • T = unknown

        The expected shape of each decoded value.

      Parameters

      • subject: string

        The subject (may contain * / > wildcards).

      • Optionalopts: { queue?: string }

        Optional queue group name for load-balanced delivery.

      Returns AsyncIterable<NatsJsonMessage<T>, any, any> & AsyncDisposable

      An async iterable of { subject, reply?, value }, also an AsyncDisposable.

      1.0.2