Skip to content

41north/natsutil.go

Repository files navigation

natsutil.go

Build Coverage Status License

Status: EXPERIMENTAL

A collection of utilities for working with NATS.io.

Documentation

GoDoc

Full go doc style documentation for the project can be viewed online without installing this package by using the excellent GoDoc site here: http://godoc.org/github.com/41north/natsutil.go

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/41north/natsutil.go

Installation

$ go get -u github.com/41north/natsutil.go

Add this import line to the file you're working in:

import natsutil "github.com/41north/natsutil.go"

Quick Start

The following utilities will be of interest:

Subject Builder

A builder much like strings.Builder which helps with constructing valid subject names and ensures invalid characters are not used:

sb := natsutil.SubjectBuilder{}

// 'foo.bar.baz.*'
sb.Push("foo", "bar", "baz")
sb.Star()
subject := sb.String()

// 'foo.bar'
sb.Pop(2)
subject = sb.String()

// 'foo.bar.hello.>'
sb.Push("hello")
sb.Chevron()
subject = sb.String()

Generic Key Value Store

A generic interface for interacting with JetStream Key-Value stores can be created with the following:

type testPayload struct {
	Value int `json:""`
}

// create a nats connection
nc, err := nats.Connect(s.ClientURL(), opts...)
...

// get a JetStream context
js, err := nc.JetStream(nats.MaxWait(10 * time.Second))
...

// get a reference to the nats KV interface
kv, err := js.KeyValue("my-bucket")
...

// create a generic KeyValue interface that uses JSON encoding
encoder := builtin.JsonEncoder{}
kvT = natsutil.NewKeyValue[testPayload](kv, &codec)

// put a testPayload object
kvT.Put("foo", testPayload{1})

// get a testPayload object
get, err := kvT.Get("foo")

// create a generic Watcher
watcher, err := kvT.WatchAll()
...

License

Go-async is licensed under the Apache 2.0 License

Contact

If you want to get in touch drop us an email at [email protected]

Releases

No releases published

Packages

No packages published