Skip to content

Instantly share code, notes, and snippets.

@nguyenthang98
Created May 8, 2021 14:38
Show Gist options
  • Save nguyenthang98/88fab0a7d021929669044e9f5e6fa171 to your computer and use it in GitHub Desktop.
Save nguyenthang98/88fab0a7d021929669044e9f5e6fa171 to your computer and use it in GitHub Desktop.
tmp
package main
import stan "github.com/nats-io/stan.go"
import "github.com/nats-io/nats.go"
import "fmt"
func main() {
nc, err := nats.Connect("tls://localhost:4443", nats.ClientCert("/client-cert.pem", "/client-key.pem"), nats.RootCAs("/client-cert.pem"))
if err != nil {
fmt.Printf("Error connect to nats %v", err)
return
}
sc, err := stan.Connect("test-cluster", "client-123", stan.NatsConn(nc))
if err != nil {
fmt.Printf("Error connect to nats %v", err)
return
}
// Simple Synchronous Publisher
sc.Publish("foo", []byte("Hello World")) // does not return until an ack has been received from NATS Streaming
// Simple Async Subscriber
sub, _ := sc.Subscribe("foo", func(m *stan.Msg) {
fmt.Printf("Received a message: %s\n", string(m.Data))
})
// Unsubscribe
sub.Unsubscribe()
// Close connection
sc.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment