Nats
Re-exporting the most used types and functions from the nats.ws
package.
Example¶
See¶
- https://docs.nats.io/using-nats/developer
- https://github.com/nats-io/nats.ws#readme
Classes¶
NatsError¶
Extends¶
Constructors¶
new NatsError()¶
Parameters¶
message¶
string
code¶
string
chainedError?¶
Returns¶
Api¶
private
Overrides¶
Error.constructor
Properties¶
api_error?¶
chainedError?¶
code¶
message¶
Overrides¶
Error.message
name¶
Overrides¶
Error.name
permissionContext?¶
operation¶
queue?¶
subject¶
stack?¶
Inherited from¶
Error.stack
prepareStackTrace()?¶
Optional override for formatting stack traces
Parameters¶
err¶
stackTraces¶
CallSite
[]
Returns¶
any
See¶
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Inherited from¶
Error.prepareStackTrace
stackTraceLimit¶
Inherited from¶
Error.stackTraceLimit
Methods¶
isAuthError()¶
Returns¶
boolean
isAuthTimeout()¶
Returns¶
boolean
isJetStreamError()¶
Returns¶
boolean
isPermissionError()¶
Returns¶
boolean
isProtocolError()¶
Returns¶
boolean
jsError()¶
Returns¶
null
| ApiError
captureStackTrace()¶
Create .stack property on a target object
Parameters¶
targetObject¶
object
constructorOpt?¶
Returns¶
void
Inherited from¶
Error.captureStackTrace
errorForCode()¶
Parameters¶
code¶
string
chainedError?¶
Returns¶
Interfaces¶
Codec<T>¶
Type Parameters¶
• T
Methods¶
decode()¶
Decode an Uint8Array from a message payload into a T
Parameters¶
a¶
Uint8Array
<ArrayBufferLike
>
Returns¶
T
encode()¶
Encode T to an Uint8Array suitable for including in a message payload.
Parameters¶
d¶
T
Returns¶
Uint8Array
<ArrayBufferLike
>
Msg¶
Represents a message delivered by NATS. This interface is used by Subscribers.
Properties¶
data¶
The message’s data (or payload)
headers?¶
Possible headers that may have been set by the server or the publisher.
reply?¶
A possible subject where the recipient may publish a reply (in the cases where the message represents a request).
sid¶
The subscription ID where the message was dispatched.
subject¶
The subject the message was sent to
Methods¶
json()¶
Convenience method to parse the message payload as JSON. This method will throw an exception if there’s a parsing error;
Type Parameters¶
• T
Parameters¶
reviver?¶
ReviverFn
a reviver function
Returns¶
T
respond()¶
Convenience to publish a response to the reply subject in the
message - this is the same as doing nc.publish(msg.reply, ...)
.
Parameters¶
payload?¶
Payload
opts?¶
Returns¶
boolean
string()¶
Convenience method to parse the message payload as string. This method may throw an exception if there’s a conversion error
Returns¶
string
MsgHdrs¶
Extends¶
Iterable
<[string
,string
[]]>
Properties¶
code¶
description¶
hasError¶
status¶
Methods¶
[iterator]()¶
Returns¶
Iterator
<[string
, string
[]], any
, any
>
Inherited from¶
Iterable.[iterator]
append()¶
Parameters¶
k¶
string
v¶
string
match?¶
Match
Returns¶
void
delete()¶
Parameters¶
k¶
string
match?¶
Match
Returns¶
void
get()¶
Parameters¶
k¶
string
match?¶
Match
Returns¶
string
has()¶
Parameters¶
k¶
string
match?¶
Match
Returns¶
boolean
keys()¶
Returns¶
string
[]
last()¶
Parameters¶
k¶
string
match?¶
Match
Returns¶
string
set()¶
Parameters¶
k¶
string
v¶
string
match?¶
Match
Returns¶
void
values()¶
Parameters¶
k¶
string
match?¶
Match
Returns¶
string
[]
NatsConnection¶
Properties¶
info?¶
ServerInfo to the currently connected server or undefined
services¶
Returns a ServicesAPI which allows you to build services using NATS.
Methods¶
close()¶
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()¶
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¶
drain()¶
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()¶
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()¶
Returns the hostport of the server the client is connected to.
Returns¶
string
isClosed()¶
Returns true if the client is closed.
Returns¶
boolean
isDraining()¶
Returns true if the client is draining.
Returns¶
boolean
jetstream()¶
Returns a JetStreamClient which allows publishing messages to JetStream or consuming messages from streams.
Parameters¶
opts?¶
JetStreamManagerOptions
| JetStreamOptions
Returns¶
JetStreamClient
jetstreamManager()¶
Returns a Promise to a JetStreamManager which allows the client to access Streams and Consumers information.
Parameters¶
opts?¶
JetStreamManagerOptions
Returns¶
Promise
<JetStreamManager
>
publish()¶
Publishes the specified data to the specified subject.
Parameters¶
subject¶
string
payload?¶
Payload
options?¶
Returns¶
void
publishMessage()¶
Publishes using the subject of the specified message, specifying the data, headers and reply found in the message if any.
Parameters¶
msg¶
Returns¶
void
reconnect()¶
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()¶
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?¶
Returns¶
requestMany()¶
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
, any
, any
>>
respondMessage()¶
Replies using the reply subject of the specified message, specifying the data, headers in the message if any.
Parameters¶
msg¶
Returns¶
boolean
rtt()¶
Returns¶
Promise
<number
>
the number of milliseconds it took for a flush.
stats()¶
Returns some metrics such as the number of messages and bytes sent and recieved by the client.
Returns¶
Stats
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
, any
, any
>
an AsyncIterable
subscribe()¶
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?¶
Returns¶
PublishOptions¶
Properties¶
headers?¶
Optional headers to include with the message.
reply?¶
An optional subject where a response should be sent. Note you must have a subscription listening on this subject to receive the response.
RequestOptions¶
Properties¶
headers?¶
MsgHdrs to include with the request.
noMux?¶
If true, the request API will create a regular NATS subscription to process the response. Otherwise a shared muxed subscriptions is used. Requires reply
reply?¶
The subject where the response should be sent to. Requires noMux
timeout¶
number of milliseconds before the request will timeout.
Sub<T>¶
Basic interface to a Subscription type
Extends¶
AsyncIterable
<T
>
Type Parameters¶
• T
Properties¶
closed¶
A promise that resolves when the subscription closes
Methods¶
[asyncIterator]()¶
Returns¶
AsyncIterator
<T
, any
, any
>
Inherited from¶
AsyncIterable.[asyncIterator]
drain()¶
Drain the subscription, closing it after processing all messages currently in flight for the client. Returns a promise that resolves when the subscription finished draining.
Returns¶
Promise
<void
>
getMax()¶
Return the max number of messages before the subscription will unsubscribe.
Returns¶
undefined
| number
getPending()¶
Returns the number of messages that are pending processing. Note that this is method is only valid for iterators.
Returns¶
number
getProcessed()¶
Returns the number of messages that have been processed by the subscription.
Returns¶
number
getReceived()¶
Returns the number of messages received by the subscription.
Returns¶
number
getSubject()¶
Returns the subject that was used to create the subscription.
Returns¶
string
isClosed()¶
Returns true if the subscription is closed.
Returns¶
boolean
isDraining()¶
Returns true if the subscription is draining.
Returns¶
boolean
unsubscribe()¶
Stop the subscription from receiving messages. You can optionally specify that the subscription should stop after the specified number of messages have been received. Note this count is since the lifetime of the subscription.
Parameters¶
max?¶
number
Returns¶
void
SubOpts<T>¶
Subscription Options
Type Parameters¶
• T
Properties¶
callback()?¶
An optional function that will handle messages. Typically, messages are processed via an async iterator on the subscription. If this option is provided, messages are processed by the specified function.
Parameters¶
err¶
null
| NatsError
msg¶
T
Returns¶
void
max?¶
Optional maximum number of messages to deliver to the subscription before it is auto-unsubscribed.
queue?¶
Optional queue name (subscriptions on the same subject that use queues are horizontally load balanced when part of the same queue).
timeout?¶
Optional maximum number of milliseconds before a timer raises an error. This useful to monitor a subscription that is expected to yield messages. The timer is cancelled when the first message is received by the subscription.
Type Aliases¶
MsgRequest¶
Subscription¶
Type alias for NATS core subscriptions
SubscriptionOptions¶
Subscription Options
Functions¶
headers()¶
Parameters¶
code?¶
number
description?¶
string
Returns¶
StringCodec()¶
Returns a Codec for encoding strings to a message payload and decoding message payloads into strings.
Returns¶
Codec
<string
>
References¶
JSONCodec¶
Re-exports JSONCodec