Table of Contents

Publishing Messages to Streams

If you want to persist your messages you can do a normal publish to the subject for unacknowledged delivery, though it's better to use the JetStream context publish calls instead as the JetStream server will reply with an acknowledgement that it was successfully stored.

The subject must be configured on a stream to be persisted:

await using var nc = new NatsClient();
var js = nc.CreateJetStreamContext();

await js.CreateStreamAsync(new StreamConfig(name: "ORDERS", subjects: new[] { "orders.>" }));

or using the nats cli:

$ nats stream create ORDERS --subjects 'orders.>'

Then you can publish to subjects captured by the stream:

await using var nc = new NatsClient();
var js = nc.CreateJetStreamContext();

var order = new Order { Id = 1 };

var ack = await js.PublishAsync("orders.new.1", order);

ack.EnsureSuccess();

Message Deduplication

JetStream support idempotent message writes by ignoring duplicate messages as indicated by the message ID. Message ID is not part of the message but rather passed as metadata, part of the message headers.

await using var nc = new NatsClient();
var js = nc.CreateJetStreamContext();

var order = new Order { Id = 1 };

var ack = await js.PublishAsync(subject: "orders.new.1", data: order, opts: new NatsJSPubOpts { MsgId = "1" });
if (ack.Duplicate)
{
    // A message with the same ID was published before
}