forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
62 lines (49 loc) · 1.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show to connect and publish/subscribe for messages.
// Message are received on demand using NextMsg.
package main
import (
"log"
"time"
"github.com/nats-io/nats"
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
// Declare the subject to use for publishing/subscribing.
const subject = "test"
// Connect to the local nats server.
conn, err := nats.Connect(nats.DefaultURL)
if err != nil {
log.Println("Unable to connect to NATS")
return
}
// Subscribe to receive messages for the specified subject.
sub, err := conn.SubscribeSync(subject)
if err != nil {
log.Println("Subscribing for specified subject:", err)
return
}
// Publish the message for the specified subject.
if err := conn.Publish(subject, []byte("Hello World")); err != nil {
log.Println("Publishing a message for specified subject:", err)
return
}
// Pull the message we just published.
m, err := sub.NextMsg(time.Second)
if err != nil {
log.Println("Error pulling a message from the bus:", err)
return
}
// Display the message.
log.Println("Received a message:", string(m.Data))
// Unsubscribe from receiving these messages.
if err := sub.Unsubscribe(); err != nil {
log.Println("Error unsubscribing from the bus:", err)
return
}
// Close the connection to the NATS server.
conn.Close()
}