Method PublishAsync
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
subjectstringSubject to publish the data to.
dataTData to publish.
serializerINatsSerialize<T>Serializer to use for the message type.
optsNatsJSPubOptsPublish options.
headersNatsHeadersOptional message headers.
cancellationTokenCancellationTokenA 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
TType 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.