Skip to content

Instantly share code, notes, and snippets.

@ripienaar
Last active March 25, 2020 12:26
Show Gist options
  • Save ripienaar/e4d4bd43bb328b86b17c027afd817c58 to your computer and use it in GitHub Desktop.
Save ripienaar/e4d4bd43bb328b86b17c027afd817c58 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"github.com/nats-io/nats.go"
)
func main() {
nc, err := nats.Connect("localhost")
if err != nil {
panic(err)
}
// Subscribe sets up go routines to handle the message and call the
// function in the background and returns to your application for
// other work
nc.Subscribe("demo.hello", func(m *nats.Msg) {
fmt.Printf("Received a message: %s\n", string(m.Data))
})
// this is like a super minimal app, so all we do is pause forever
// letting the background stuff do their thing
<-context.Background().Done()
}
% go run sub.go
Received a message: test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment