Method NextAsync
NextAsync<T>(INatsDeserialize<T>?, NatsJSNextOpts?, CancellationToken)
Consume a single message from the stream using this consumer.
public ValueTask<NatsJSMsg<T>?> NextAsync<T>(INatsDeserialize<T>? serializer = null, NatsJSNextOpts? opts = null, CancellationToken cancellationToken = default)
Parameters
serializer
INatsDeserialize<T>Serializer to use for the message type.
opts
NatsJSNextOptsNext message options. (default: 30 seconds timeout)
cancellationToken
CancellationTokenA CancellationToken used to cancel the call.
Returns
Type Parameters
T
Message type to deserialize.
Examples
The following example shows how you might process messages:
var next = await consumer.NextAsync<Data>();
if (next is { } msg)
{
// process the message
await msg.AckAsync();
}
Remarks
If the request to server expires (in 30 seconds by default) this call returns NULL
.
This method is implemented as a fetch with MaxMsgs=1
which means every request will create a new subscription
on the NATS server. This would be inefficient if you're consuming a lot of messages and you should consider using
fetch or consume methods.
Exceptions
- NatsJSProtocolException
Consumer is deleted, it's push based or request sent to server is invalid.
- NatsJSException
There is an error sending the message or this consumer object isn't valid anymore because it was deleted earlier.