Created
May 8, 2021 14:38
-
-
Save nguyenthang98/88fab0a7d021929669044e9f5e6fa171 to your computer and use it in GitHub Desktop.
tmp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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