Debian Codegraph @

examples/docs/learn-jetstream-subject-mapping-republish/main.go

raw
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/nats-io/nats.go"
9 "github.com/nats-io/nats.go/jetstream"
10 )
11
12 func main() {
13 // Connect to the NATS server.
14 nc, err := nats.Connect("nats://localhost:4222")
15 if err != nil {
16 panic(err)
17 }
18 defer nc.Close()
19
20 // Create a JetStream context (the stream manager).
21 js, err := jetstream.New(nc)
22 if err != nil {
23 panic(err)
24 }
25
26 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
27 defer cancel()
28
29 // Look up the ORDERS stream.
30 stream, err := js.Stream(ctx, "ORDERS")
31 if err != nil {
32 panic(err)
33 }
34
35 // NATS-DOC-START
36 // Read the current config, add a republish rule, and push the update.
37 // Every message stored on "orders.>" is also echoed live to "dash.orders.>"
38 // so a dashboard can subscribe without touching the stream.
39 cfg := stream.CachedInfo().Config
40 cfg.RePublish = &jetstream.RePublish{
41 Source: "orders.>",
42 Destination: "dash.orders.>",
43 // HeadersOnly: true, // republish only the headers, not the body
44 }
45
46 updated, err := js.UpdateStream(ctx, cfg)
47 if err != nil {
48 panic(err)
49 }
50
51 fmt.Printf("Republish destination: %s\n", updated.CachedInfo().Config.RePublish.Destination)
52 // NATS-DOC-END
53 }