Skip to content

Commit

Permalink
add a standard subscription [ci-skip]
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Sullivan <colin@synadia.com>
  • Loading branch information
ColinSullivan1 committed Apr 7, 2022
1 parent c4cfcdf commit 632b0f8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions examples/nats-echo/main.go
Expand Up @@ -44,7 +44,7 @@ func showUsageAndExit(exitcode int) {
}

func printMsg(m *nats.Msg, i int) {
log.Printf("[#%d] Echoing to [%s]: %q", i, m.Reply, m.Data)
log.Printf("[#%d] Echoing from [%s] to [%s]: %q", i, m.Subject, m.Reply, m.Data)
}

func main() {
Expand Down Expand Up @@ -103,7 +103,7 @@ func main() {

subj, i := args[0], 0

nc.QueueSubscribe(subj, "echo", func(msg *nats.Msg) {
handleMsg := func(msg *nats.Msg) {
i++
if msg.Reply != "" {
printMsg(msg, i)
Expand All @@ -115,6 +115,14 @@ func main() {
nc.Publish(msg.Reply, msg.Data)
}
}
}

nc.QueueSubscribe(subj, "echo", func(msg *nats.Msg) {
handleMsg(msg)
})
allSubj := subj + ".all"
nc.Subscribe(allSubj, func(msg *nats.Msg) {
handleMsg(msg)
})
nc.Flush()

Expand All @@ -123,8 +131,9 @@ func main() {
}

log.Printf("Echo Service listening on [%s]\n", subj)
log.Printf("Echo Service (All) listening on [%s]\n", allSubj)

// Now handle signal to terminate so we cam drain on exit.
// Now handle signal to terminate so we can drain on exit.
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT)

Expand Down

0 comments on commit 632b0f8

Please sign in to comment.