-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (35 loc) · 901 Bytes
/
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
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
const channelName = "exampleChannel"
func main() {
rdb := openConnection()
pubsub := rdb.Subscribe(context.Background(), channelName)
defer pubsub.Close()
go func() {
receiverMessage(pubsub)
}()
err := rdb.Publish(context.Background(), channelName, "PubSub Example Message").Err()
if err != nil {
fmt.Printf("There was an error in publish. Error: %s", err.Error())
} else {
fmt.Print("Message published.")
}
}
func openConnection() *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
return rdb
}
func receiverMessage(pubsub *redis.PubSub) {
message, err := pubsub.ReceiveMessage(context.Background())
if err != nil {
fmt.Printf("There was an error in receive. Error: %s", err.Error())
} else {
fmt.Printf("Message received. Message: %s", message.Payload)
}
}