-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_subscribe.go
59 lines (45 loc) · 962 Bytes
/
publish_subscribe.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
package main
import (
"fmt"
pubnub "github.com/pubnub/go"
)
var pn *pubnub.PubNub
func init() {
config := pubnub.NewConfig()
config.SubscribeKey = "demo"
config.PublishKey = "demo"
pn = pubnub.NewPubNub(config)
}
func main() {
listener := pubnub.NewListener()
doneSubscribe := make(chan bool)
data := make(map[string]interface{})
data["awesome"] = "data"
go func() {
for {
select {
case status := <-listener.Status:
switch status.Category {
case pubnub.PNConnectedCategory:
res, status, err := pn.Publish().
Channel("awesome-channel").
Message(data).
Execute()
fmt.Printf(res, status, err)
doneSubscribe <- true
return
}
case <-listener.Message:
case <-listener.Presence:
}
}
}()
pn.AddListener(listener)
pn.Subscribe().
Channels([]string{"awesome-channel"}).
Execute()
<-doneSubscribe
pn.Unsubscribe().
Channels([]string{"awesome-channel"}).
Execute()
}