| 413 | } |
| 414 | |
| 415 | func ExamplePubOpt() { |
| 416 | nc, err := nats.Connect("localhost") |
| 417 | if err != nil { |
| 418 | log.Fatal(err) |
| 419 | } |
| 420 | |
| 421 | // Create JetStream context to produce/consumer messages that will be persisted. |
| 422 | js, err := nc.JetStream() |
| 423 | if err != nil { |
| 424 | log.Fatal(err) |
| 425 | } |
| 426 | |
| 427 | // Create stream to persist messages published on 'foo'. |
| 428 | js.AddStream(&nats.StreamConfig{ |
| 429 | Name: "FOO", |
| 430 | Subjects: []string{"foo"}, |
| 431 | }) |
| 432 | |
| 433 | // Publish is synchronous by default, and waits for a PubAck response. |
| 434 | js.Publish("foo", []byte("Hello JS!")) |
| 435 | |
| 436 | // Publish with a custom timeout. |
| 437 | js.Publish("foo", []byte("Hello JS!"), nats.AckWait(500*time.Millisecond)) |
| 438 | |
| 439 | // Publish with a context. |
| 440 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 441 | defer cancel() |
| 442 | |
| 443 | js.Publish("foo", []byte("Hello JS!"), nats.Context(ctx)) |
| 444 | |
| 445 | // Publish and assert the expected stream name. |
| 446 | js.Publish("foo", []byte("Hello JS!"), nats.ExpectStream("FOO")) |
| 447 | |
| 448 | // Publish and assert the last sequence. |
| 449 | js.Publish("foo", []byte("Hello JS!"), nats.ExpectLastSequence(5)) |
| 450 | |
| 451 | // Publish and tag the message with an ID. |
| 452 | js.Publish("foo", []byte("Hello JS!"), nats.MsgId("foo:6")) |
| 453 | |
| 454 | // Publish and assert the last msg ID. |
| 455 | js.Publish("foo", []byte("Hello JS!"), nats.ExpectLastMsgId("foo:6")) |
| 456 | } |
| 457 | |
| 458 | func ExampleSubOpt() { |
| 459 | nc, err := nats.Connect("localhost") |