Skip to content

NatsConnection

@wuespace/telestion / nats / NatsConnection

Properties

info?

info?: ServerInfo

ServerInfo to the currently connected server or undefined


services

services: ServicesAPI

Returns a ServicesAPI which allows you to build services using NATS.

Methods

close()

close(): Promise<void>

Close will close the connection to the server. This call will terminate all pending requests and subscriptions. The returned promise resolves when the connection closes.

Returns

Promise<void>


closed()

closed(): Promise<void | Error>

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>


drain()

drain(): Promise<void>

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>


flush()

flush(): 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>


getServer()

getServer(): string

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

Returns

string


isClosed()

isClosed(): boolean

Returns true if the client is closed.

Returns

boolean


isDraining()

isDraining(): boolean

Returns true if the client is draining.

Returns

boolean


jetstream()

jetstream(opts?): JetStreamClient

Returns a JetStreamClient which allows publishing messages to JetStream or consuming messages from streams.

Parameters

opts?: JetStreamOptions

Returns

JetStreamClient


jetstreamManager()

jetstreamManager(opts?): Promise<JetStreamManager>

Returns a Promise to a JetStreamManager which allows the client to access Streams and Consumers information.

Parameters

opts?: JetStreamManagerOptions

Returns

Promise<JetStreamManager>


publish()

publish(subject, payload?, options?): void

Publishes the specified data to the specified subject.

Parameters

subject: string

payload?: Payload

options?: PublishOptions

Returns

void


reconnect()

reconnect(): Promise<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>


request()

request(subject, payload?, opts?): Promise<Msg>

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

subject: string

payload?: Payload

opts?: RequestOptions

Returns

Promise<Msg>


requestMany()

requestMany(subject, payload?, opts?): Promise<AsyncIterable<Msg>>

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

Parameters

subject: string

payload?: Payload

opts?: Partial<RequestManyOptions>

Returns

Promise<AsyncIterable<Msg>>


rtt()

rtt(): Promise<number>

Returns

Promise<number>

the number of milliseconds it took for a flush.


stats()

stats(): Stats

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

Returns

Stats


status()

status(): AsyncIterable<Status>

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


subscribe()

subscribe(subject, opts?): Subscription

Subscribe expresses interest in the specified subject. The subject may have wildcards. Messages are delivered to the SubscriptionOptions callback if specified. Otherwise, the subscription is an async iterator for Msg.

Parameters

subject: string

opts?: SubscriptionOptions

Returns

Subscription