diff --git a/Midware/NATS/Client/Golang/natspub.go b/Midware/NATS/Client/Golang/natspub.go new file mode 100644 index 0000000..ca18847 --- /dev/null +++ b/Midware/NATS/Client/Golang/natspub.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "time" + + "github.com/nats-io/nats.go" +) + +func main() { + nc, err := nats.Connect(nats.DefaultURL) + defer nc.Close() + if nil != err { + fmt.Println(err) + return + } + + for { + nc.Publish("foo", []byte("Hello Publish!")) + time.Sleep(1 * time.Second) + } +} diff --git a/Midware/NATS/Client/Golang/natsreq.go b/Midware/NATS/Client/Golang/natsreq.go new file mode 100644 index 0000000..6dbf6f3 --- /dev/null +++ b/Midware/NATS/Client/Golang/natsreq.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "time" + + "github.com/nats-io/nats.go" +) + +func main() { + nc, err := nats.Connect(nats.DefaultURL) + defer nc.Close() + if nil != err { + fmt.Println(err) + return + } + + for { + m, err := nc.Request("foo", []byte("Hello Request!"), 1*time.Second) + if nil != err { + fmt.Println(err) + return + } + fmt.Printf("Received a Response: %s\n", string(m.Data)) + time.Sleep(1 * time.Second) + } +} diff --git a/Midware/NATS/Client/Golang/natsrply.go b/Midware/NATS/Client/Golang/natsrply.go new file mode 100644 index 0000000..bc1faff --- /dev/null +++ b/Midware/NATS/Client/Golang/natsrply.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "sync" + + "github.com/nats-io/nats.go" +) + +func main() { + nc, err := nats.Connect(nats.DefaultURL) + defer nc.Close() + if nil != err { + fmt.Println(err) + return + } + wg := sync.WaitGroup{} + wg.Add(10) + + nc.Subscribe("foo", func(m *nats.Msg) { + fmt.Printf("Received a message: %s\n", string(m.Data)) + m.Respond(m.Data) + wg.Done() + }) + wg.Wait() +} diff --git a/Midware/NATS/Client/Golang/natssub.go b/Midware/NATS/Client/Golang/natssub.go new file mode 100644 index 0000000..754ceed --- /dev/null +++ b/Midware/NATS/Client/Golang/natssub.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "sync" + + "github.com/nats-io/nats.go" +) + +func main() { + nc, err := nats.Connect(nats.DefaultURL) + defer nc.Close() + if nil != err { + fmt.Println(err) + return + } + wg := sync.WaitGroup{} + wg.Add(10) + + nc.Subscribe("foo", func(m *nats.Msg) { + fmt.Printf("Received a message: %s\n", string(m.Data)) + wg.Done() + }) + wg.Wait() +}