| 1 | # Legacy JetStream API |
| 2 | |
| 3 | This is a documentation for the legacy JetStream API. A README for the current |
| 4 | API can be found [here](jetstream/README.md) |
| 5 | |
| 6 | ## JetStream Basic Usage |
| 7 | |
| 8 | ```go |
| 9 | import "github.com/nats-io/nats.go" |
| 10 | |
| 11 | // Connect to NATS |
| 12 | nc, _ := nats.Connect(nats.DefaultURL) |
| 13 | |
| 14 | // Create JetStream Context |
| 15 | js, _ := nc.JetStream(nats.PublishAsyncMaxPending(256)) |
| 16 | |
| 17 | // Simple Stream Publisher |
| 18 | js.Publish("ORDERS.scratch", []byte("hello")) |
| 19 | |
| 20 | // Simple Async Stream Publisher |
| 21 | for i := 0; i < 500; i++ { |
| 22 | js.PublishAsync("ORDERS.scratch", []byte("hello")) |
| 23 | } |
| 24 | select { |
| 25 | case <-js.PublishAsyncComplete(): |
| 26 | case <-time.After(5 * time.Second): |
| 27 | fmt.Println("Did not resolve in time") |
| 28 | } |
| 29 | |
| 30 | // Simple Async Ephemeral Consumer |
| 31 | js.Subscribe("ORDERS.*", func(m *nats.Msg) { |
| 32 | fmt.Printf("Received a JetStream message: %s\n", string(m.Data)) |
| 33 | }) |
| 34 | |
| 35 | // Simple Sync Durable Consumer (optional SubOpts at the end) |
| 36 | sub, err := js.SubscribeSync("ORDERS.*", nats.Durable("MONITOR"), nats.MaxDeliver(3)) |
| 37 | m, err := sub.NextMsg(timeout) |
| 38 | |
| 39 | // Simple Pull Consumer |
| 40 | sub, err := js.PullSubscribe("ORDERS.*", "MONITOR") |
| 41 | msgs, err := sub.Fetch(10) |
| 42 | |
| 43 | // Unsubscribe |
| 44 | sub.Unsubscribe() |
| 45 | |
| 46 | // Drain |
| 47 | sub.Drain() |
| 48 | ``` |
| 49 | |
| 50 | ## JetStream Basic Management |
| 51 | |
| 52 | ```go |
| 53 | import "github.com/nats-io/nats.go" |
| 54 | |
| 55 | // Connect to NATS |
| 56 | nc, _ := nats.Connect(nats.DefaultURL) |
| 57 | |
| 58 | // Create JetStream Context |
| 59 | js, _ := nc.JetStream() |
| 60 | |
| 61 | // Create a Stream |
| 62 | js.AddStream(&nats.StreamConfig{ |
| 63 | Name: "ORDERS", |
| 64 | Subjects: []string{"ORDERS.*"}, |
| 65 | }) |
| 66 | |
| 67 | // Update a Stream |
| 68 | js.UpdateStream(&nats.StreamConfig{ |
| 69 | Name: "ORDERS", |
| 70 | MaxBytes: 8, |
| 71 | }) |
| 72 | |
| 73 | // Create a Consumer |
| 74 | js.AddConsumer("ORDERS", &nats.ConsumerConfig{ |
| 75 | Durable: "MONITOR", |
| 76 | }) |
| 77 | |
| 78 | // Delete Consumer |
| 79 | js.DeleteConsumer("ORDERS", "MONITOR") |
| 80 | |
| 81 | // Delete Stream |
| 82 | js.DeleteStream("ORDERS") |
| 83 | ``` |