Method GetConsumerAsync
GetConsumerAsync(string, string, CancellationToken)
Gets consumer information from the server and creates a NATS JetStream consumer NatsJSConsumer.
public ValueTask<INatsJSConsumer> GetConsumerAsync(string stream, string consumer, CancellationToken cancellationToken = default)
Parameters
streamstringStream name where consumer is associated to.
consumerstringConsumer name.
cancellationTokenCancellationTokenA CancellationToken used to cancel the API call.
Returns
- ValueTask<INatsJSConsumer>
The NATS JetStream consumer object which can be used retrieving data from the stream.
Remarks
Warning: This method issues a $JS.API.CONSUMER.INFO request to the server on every call.
Calling it frequently (e.g., in a message-processing loop or on a short timer) can cause significant load
on the NATS cluster, lead to API timeouts, and degrade performance for all clients.
If you need to track consumer progress at runtime (e.g., pending message count, sequence numbers, or delivery attempts),
use Metadata on each received message instead. When available, it provides
NumPending, NumDelivered,
Sequence, and Timestamp
without requiring a server round-trip. Note that Metadata can be null
(for example, if the reply subject cannot be parsed), so callers should always check for null before
accessing its properties.
Prefer using CreateOrUpdateConsumerAsync(string, ConsumerConfig, CancellationToken) or CreateConsumerAsync(string, ConsumerConfig, CancellationToken) to obtain a consumer handle. Reserve this method for cases where you need to retrieve a consumer that was already created separately.
Exceptions
- NatsJSException
There was an issue retrieving the response.
- NatsJSApiException
Server responded with an error.
- ArgumentException
The
streamname is invalid.- ArgumentNullException
The
streamname isnull.