MCPcopy
hub / github.com/nats-io/nats.go / ExampleAckOpt

Function ExampleAckOpt

example_test.go:690–721  ·  view source on GitHub ↗

AckOpt are the options that can be passed when acknowledge a message.

()

Source from the content-addressed store, hash-verified

688
689// AckOpt are the options that can be passed when acknowledge a message.
690func ExampleAckOpt() {
691 nc, err := nats.Connect("localhost")
692 if err != nil {
693 log.Fatal(err)
694 }
695
696 // Create JetStream context to produce/consumer messages that will be persisted.
697 js, err := nc.JetStream()
698 if err != nil {
699 log.Fatal(err)
700 }
701
702 // Create stream to persist messages published on 'foo'.
703 js.AddStream(&nats.StreamConfig{
704 Name: "FOO",
705 Subjects: []string{"foo"},
706 })
707
708 // Publish is synchronous by default, and waits for a PubAck response.
709 js.Publish("foo", []byte("Hello JS!"))
710
711 sub, _ := js.SubscribeSync("foo")
712 msg, _ := sub.NextMsg(2 * time.Second)
713
714 // Ack and wait for 2 seconds
715 msg.InProgress(nats.AckWait(2))
716
717 // Using a context.
718 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
719 defer cancel()
720 msg.Ack(nats.Context(ctx))
721}
722
723func ExamplePullOpt() {
724 nc, err := nats.Connect("localhost")

Callers

nothing calls this directly

Calls 9

ConnectMethod · 0.80
JetStreamMethod · 0.80
NextMsgMethod · 0.80
AddStreamMethod · 0.65
PublishMethod · 0.65
SubscribeSyncMethod · 0.65
InProgressMethod · 0.65
AckMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected