Table of Contents

Method SubscribeAsync

Namespace
NATS.Client.Core
Assembly
NATS.Client.Core.dll

SubscribeAsync<T>(string, string?, INatsDeserialize<T>?, NatsSubOpts?, CancellationToken)

Initiates a subscription to a subject, optionally joining a distributed queue group.

IAsyncEnumerable<NatsMsg<T>> SubscribeAsync<T>(string subject, string? queueGroup = null, INatsDeserialize<T>? serializer = null, NatsSubOpts? opts = null, CancellationToken cancellationToken = default)

Parameters

subject string

The subject name to subscribe to.

queueGroup string

If specified, the subscriber will join this queue group.

serializer INatsDeserialize<T>

Serializer to use for the message type.

opts NatsSubOpts

A NatsSubOpts for subscription options.

cancellationToken CancellationToken

A CancellationToken used to cancel the command.

Returns

IAsyncEnumerable<NatsMsg<T>>

An asynchronous enumerable of NatsMsg<T> objects

Type Parameters

T

Specifies the type of data that may be received from the NATS Server.

Remarks

Subscribers with the same queue group name, become a queue group, and only one randomly chosen subscriber of the queue group will consume a message each time a message is received by the queue group.

The subscription is not established until the returned enumerable is iterated. To find out when the subscription has been established, set OnSubscribed using Events, or use SubscribeCoreAsync<T>(string, string?, INatsDeserialize<T>?, NatsSubOpts?, CancellationToken) which completes once the subscription has been established.