Skip to content

Instantly share code, notes, and snippets.

@shijuvar
Last active October 14, 2017 13:19
Show Gist options
  • Save shijuvar/504f4a81fd3c05ca7ed4708be7592c26 to your computer and use it in GitHub Desktop.
Save shijuvar/504f4a81fd3c05ca7ed4708be7592c26 to your computer and use it in GitHub Desktop.
A NATS Streaming client that subscribe messages from a channel “order-notification”
package main
import (
"encoding/json"
"log"
"runtime"
"time"
stan "github.com/nats-io/go-nats-streaming"
"github.com/shijuvar/gokit/examples/nats-streaming/pb"
)
const (
clusterID = "test-cluster"
clientID = "restaurant-service"
channel = "order-notification"
durableID = "restaurant-service-durable"
)
func main() {
sc, err := stan.Connect(
clusterID,
clientID,
stan.NatsURL(stan.DefaultNatsURL),
)
if err != nil {
log.Fatal(err)
}
// Subscribe with manual ack mode, and set AckWait to 60 seconds
aw, _ := time.ParseDuration("60s")
sc.Subscribe(channel, func(msg *stan.Msg) {
msg.Ack() // Manual ACK
order := pb.Order{}
// Unmarshal JSON that represents the Order data
err := json.Unmarshal(msg.Data, &order)
if err != nil {
log.Print(err)
return
}
// Handle the message
log.Printf("Subscribed message from clientID - %s for Order: %+v\n", clientID, order)
}, stan.DurableName(durableID),
stan.MaxInflight(25),
stan.SetManualAckMode(),
stan.AckWait(aw),
)
runtime.Goexit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment