Skip to content

Instantly share code, notes, and snippets.

@csthompson
Last active September 11, 2020 22:57
Show Gist options
  • Save csthompson/2df847d37b7268cd6759095686c702e7 to your computer and use it in GitHub Desktop.
Save csthompson/2df847d37b7268cd6759095686c702e7 to your computer and use it in GitHub Desktop.
NATS Publisher
package main
import (
"log"
"time"
nats "github.com/nats-io/nats.go"
)
type Msg struct {
Subject string
Body []byte
}
func main() {
nc, _ := nats.Connect(nats.DefaultURL)
ec, _ := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
msg := Msg{
Subject: "Hello World",
Body: []byte("Hello!"),
}
log.Println("Running publisher")
for {
ec.Publish("heartbeat", msg)
time.Sleep(time.Second * 5)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment