Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nsurfer committed Mar 31, 2021
1 parent 1114ca8 commit bf06b86
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ nc.Publish("foo.bar.baz", []byte("Hello World"))
nc.QueueSubscribe("foo", "job_workers", func(_ *Msg) {
received += 1;
})

```

## Advanced Usage
Expand Down Expand Up @@ -423,6 +422,82 @@ resp := &response{}
err := c.RequestWithContext(ctx, "foo", req, resp)
```

## JetStream Basic Usage

```go
import nats "github.com/nats-io/nats.go"

// Connect to NATS
nc, _ := nats.Connect(nats.DefaultURL)

// Create JetStream Context
js, _ := nc.JetStream()

// Simple Stream Publisher
js.Publish("ORDERS.scratch", []byte("hello"))

// Simple Async Stream Publisher
paf, err := js.PublishAsync("ORDERS.scratch", []byte("hello"))
select {
case pa := <-paf.Ok():
fmt.Printf("Got the PubAck: %+v\n", pa)
case err := <-paf.Err():
fmt.Printf("Got an err: %v\n", err)
// Error, resend message.
js.PublishMsg(paf.Msg())
case <-time.After(time.Second):
fmt.Println("Did not resolve in time")
}

// Simple Async Ephemeral Consumer
js.Subscribe("ORDERS.*", func(m *nats.Msg) {
fmt.Printf("Received a JetStream message: %s\n", string(m.Data))
})

// Simple Sync Durable Consumer (optional SubOpts at the end)
sub, err := js.SubscribeSync("ORDERS.*", nats.Durable("MONITOR"), nats.MaxDeliver(3))
m, err := sub.NextMsg(timeout)

// Simple Pull Consumer
sub, err := js.PullSubscribe("ORDERS.*", "MONITOR")
msgs, err := sub.Fetch(10)

// Unsubscribe
sub.Unsubscribe()

// Drain
sub.Drain()
```

## JetStream Basic Management

```go
import nats "github.com/nats-io/nats.go"

// Connect to NATS
nc, _ := nats.Connect(nats.DefaultURL)

// Create JetStream Context
js, _ := nc.JetStream()

// Create a Stream
js.AddStream(&nats.StreamConfig{
Name: "ORDERS",
Subjects: []string{"ORDERS.*"},
})

// Create a Consumer
js.AddConsumer("ORDERS", &nats.ConsumerConfig{
Durable: "MONITOR",
})

// Delete Consumer
js.DeleteConsumer("ORDERS", "MONITOR")

// Delete Stream
js.DeleteStream("ORDERS")
```

## License

Unless otherwise noted, the NATS source files are distributed
Expand Down

0 comments on commit bf06b86

Please sign in to comment.