From fabfbfe1744e57c28be54a9888797788141119e3 Mon Sep 17 00:00:00 2001 From: jar3b Date: Mon, 14 Sep 2020 16:07:48 +0300 Subject: [PATCH] chore: update readme, add NATS example --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index fae2918..3ac7ec4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,42 @@ # nacl + Client helper for NATS/STAN + +## examples + +connect to NATS + +```go +package main + +import ( + "fmt" + "github.com/jar3b/grawt" + "github.com/jar3b/nacl" + "github.com/kelseyhightower/envconfig" +) + +func main() { + // get params + conf := config.NewConfig() + + // init waiter (github.com/jar3b/grawt) + var waiter = grawt.NewWaiter() + + // init nats + var subscriptions []*nacl.NatsSubscription + if err := nacl.SetupNats(conf.Nats.Host, conf.Nats.Port, conf.Nats.User, conf.Nats.Pass, + // handler called before app closed + // we need to terminate sub's properly and sometimes doing another actions (finalizers) + waiter.AddCloseHandler(func() { + nacl.FinalizeNats(&subscriptions) + }, false), + ); err != nil { + waiter.Halt(fmt.Errorf("cannot connect to nats: %v", err)) + } + defer nacl.NatsClient.Close() + + // here we add some subscriptions and wait (using blocking call) + // ... +} +``` \ No newline at end of file