Table of Contents

Method TryPublishAsync

Namespace
NATS.Client.JetStream
Assembly
NATS.Client.JetStream.dll

TryPublishAsync<T>(string, T?, INatsSerialize<T>?, NatsJSPubOpts?, NatsHeaders?, CancellationToken)

Tries to send data to a stream associated with the subject.

ValueTask<NatsResult<PubAckResponse>> TryPublishAsync<T>(string subject, T? data, INatsSerialize<T>? serializer = null, NatsJSPubOpts? opts = null, NatsHeaders? headers = null, CancellationToken cancellationToken = default)

Parameters

subject string

Subject to publish the data to.

data T

Data to publish.

serializer INatsSerialize<T>

Serializer to use for the message type.

opts NatsJSPubOpts

Publish options.

headers NatsHeaders

Optional message headers.

cancellationToken CancellationToken

A CancellationToken used to cancel the publishing call or the wait for response.

Returns

ValueTask<NatsResult<PubAckResponse>>

The ACK response to indicate if stream accepted the message as well as additional information like the sequence number of the message stored by the stream.

Type Parameters

T

Type of the data being sent.

Remarks

Use this method to avoid exceptions.

Note that if the subject isn't backed by a stream or the connected NATS server isn't running with JetStream enabled, this call will hang waiting for an ACK until the request times out.

Exceptions

NatsJSException

There was a problem receiving the response.