@nats-io/nats-core
    Preparing search index...

    Interface NatsConnection

    interface NatsConnection {
        info?: ServerInfo;
        "[asyncDispose]"(): Promise<void>;
        close(): Promise<void>;
        closed(): Promise<void | Error>;
        drain(): Promise<void>;
        flush(): Promise<void>;
        getServer(): string;
        getServers(): readonly Server[];
        isClosed(): boolean;
        isDraining(): boolean;
        publish(subject: string, payload?: Payload, options?: PublishOptions): void;
        publishMessage(msg: Msg): void;
        reconnect(): Promise<void>;
        request(
            subject: string,
            payload?: Payload,
            opts?: RequestOptions,
        ): Promise<Msg>;
        requestMany(
            subject: string,
            payload?: Payload,
            opts?: Partial<RequestManyOptions>,
        ): Promise<AsyncIterable<Msg, any, any>>;
        respondMessage(msg: Msg): boolean;
        rtt(): Promise<number>;
        setServers(servers: string[]): void;
        stats(): Stats;
        status(): AsyncIterable<Status>;
        subscribe(subject: string, opts?: SubscriptionOptions): Subscription;
    }
    Index
    info?: ServerInfo

    ServerInfo to the currently connected server or undefined

    • Implements the AsyncDisposable protocol so the connection can be used with await using. Equivalent to calling drain; if the connection is already closed or draining, resolves without error.

      Returns Promise<void>

    • Close will close the connection to the server. This call will terminate all pending requests and subscriptions. Data pending to be sent is flushed first, but a connection that won't drain (for example, the server stopped reading) is closed after 2 seconds without waiting further. The returned promise resolves when the connection closes.

      Returns Promise<void>

    • Returns a promise that can be used to monitor if the client closes. The promise can resolve an Error if the reason for the close was an error. Note that the promise doesn't reject, but rather resolves to the error if there was one.

      Returns Promise<void | Error>

    • Initiates a drain on the connection and returns a promise that resolves when the drain completes and the connection closes.

      Drain is an ordered shutdown of the client. Instead of abruptly closing the client, subscriptions are drained, that is messages not yet processed by a subscription are handled before the subscription is closed. After subscriptions are drained it is not possible to create a new subscription. Then all pending outbound messages are sent to the server. Finally, the connection is closed.

      Returns Promise<void>

    • Returns a Promise that resolves when the client receives a reply from the server. Use of this API is not necessary by clients.

      Returns Promise<void>

    • Returns the hostport of the server the client is connected to.

      Returns string

    • Returns the current server pool, including gossiped entries received from the cluster. Each entry exposes gossiped, reconnects, lastConnect, and didConnect for filtering and diagnostics.

      To modify the pool, pass it back to setServers as listens: nc.setServers(nc.getServers().map((s) => s.listen)). The servers are unchanged and keep their state; with wsconnect, a listen keeps its entry's URL (scheme and path). s.src is the full URL.

      Returns readonly Server[]

    • Returns true if the client is closed.

      Returns boolean

    • Returns true if the client is draining.

      Returns boolean

    • Publishes the specified data to the specified subject.

      Parameters

      Returns void

    • Publishes using the subject of the specified message, specifying the data, headers and reply found in the message if any.

      Parameters

      Returns void

    • Use of this API is experimental, and it is subject to be removed.

      reconnect() enables a client to force a reconnect. A reconnect will disconnect the client, and possibly initiate a reconnect to the cluster. Note that all reconnect caveats apply:

      • If the reconnection policy given to the client doesn't allow reconnects, the connection will close.

      • Messages that are inbound or outbound could be lost.

      • All requests that are in flight will be rejected.

      Note that the returned promise will reject if the client is already closed, or if it is in the process of draining. If the client is currently disconnected, this API has no effect, as the client is already attempting to reconnect.

      Returns Promise<void>

    • Publishes a request with specified data in the specified subject expecting a response before RequestOptions#timeout milliseconds. The api returns a Promise that resolves when the first response to the request is received. If there are no responders (a subscription) listening on the request subject, the request will fail as soon as the server processes it.

      Parameters

      Returns Promise<Msg>

    • Publishes a request expecting multiple responses back. Several strategies to determine when the request should stop gathering responses.

      Parameters

      Returns Promise<AsyncIterable<Msg, any, any>>

    • Replies using the reply subject of the specified message, specifying the data, headers in the message if any.

      Parameters

      Returns boolean

    • Returns Promise<number>

      the number of milliseconds it took for a flush.

    • Replace the client's server pool with the provided list, handled as the servers option is. If the client is connected to a server that is on the new list, the connection is kept; if its server isn't on the list, the client reconnects onto the new list. Call reconnect only to move off a server that is still on the list.

      Over TCP the entries are host:port, and TLS comes from the tls option. With wsconnect an entry is a URL as in the servers option (wss://host:port/path, ws://host:port); a bare host:port that is already in the pool keeps that entry's URL, and a new one is wss.

      A server already in the pool keeps its state (reconnect count, last connect) when it is listed again as the same server: the same host:port over TCP, the same URL or a bare host:port with wsconnect. A different URL for the same host:port is a new endpoint and starts over.

      Throws if servers is empty or contains invalid entries. Removing all reachable servers from the pool can leave the client unable to reconnect.

      Parameters

      • servers: string[]

      Returns void

    • Returns some metrics such as the number of messages and bytes sent and recieved by the client.

      Returns Stats

    • Returns an async iterator of Status that may be useful in understanding when the client looses a connection, or reconnects, or receives an update from the cluster among other things.

      Returns AsyncIterable<Status>

      an AsyncIterable