/
main.go
58 lines (47 loc) · 1.19 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
package main
import (
"context"
"flag"
"fmt"
"github.com/genkami/kiara"
adapter "github.com/genkami/kiara/adapter/redis"
"github.com/genkami/watson"
"github.com/go-redis/redis/v8"
)
type Message struct {
From string
Body string
}
type WatsonCodec struct{}
func (_ *WatsonCodec) Marshal(v interface{}) ([]byte, error) {
return watson.Marshal(v)
}
func (_ *WatsonCodec) Unmarshal(src []byte, v interface{}) error {
return watson.Unmarshal(src, v)
}
func main() {
var redisAddr string
flag.StringVar(&redisAddr, "redis-addr", "localhost:6379", "Redis address")
flag.Parse()
var err error
redisClient := redis.NewClient(&redis.Options{Addr: redisAddr})
pubsub := kiara.NewPubSub(
adapter.NewAdapter(redisClient),
kiara.WithCodec(&WatsonCodec{}),
)
defer pubsub.Close()
channel := make(chan Message, 5)
sub, err := pubsub.Subscribe("room:123", channel)
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
ctx := context.Background()
msg := &Message{From: "Amelia", Body: "Do you know that this message was marshaled into WATSON?"}
err = pubsub.Publish(ctx, "room:123", msg)
if err != nil {
panic(err)
}
sent := <-channel
fmt.Printf("%s: %s\n", sent.From, sent.Body)
}