Table of Contents

Method PublishAsync

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

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

Sends data to a stream associated with the subject.

public ValueTask<PubAckResponse> PublishAsync<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<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

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.

By setting msgId you can ensure messages written to a stream only once. JetStream support idempotent message writes by ignoring duplicate messages as indicated by the Nats-Msg-Id header. If both msgId and the Nats-Msg-Id header value was set, msgId parameter value will be used.

Exceptions

NatsJSException

There was a problem receiving the response.