-
Notifications
You must be signed in to change notification settings - Fork 0
/
onboarding.go
100 lines (83 loc) · 2.26 KB
/
onboarding.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
pubnub "github.com/pubnub/go"
)
func main() {
// Config
config := pubnub.NewConfig()
config.PublishKey = "demo"
config.SubscribeKey = "demo"
// End Config
// Init
pn := pubnub.NewPubNub(config)
// End Init
// Init Listener
listener := pubnub.NewListener()
waitForConnect := make(chan bool)
go func() {
for {
select {
case status := <-listener.Status:
switch status.Category {
case pubnub.PNConnectedCategory:
// Connect event. You can do stuff like publish, and know you'll get it.
// Or just use the connected event to confirm you are subscribed for
// UI / internal notifications, etc
waitForConnect <- true
}
case msg := <-listener.Message:
fmt.Println(" --- MESSAGE: ")
fmt.Println(fmt.Sprintf("msg.Channel: %s", msg.Channel))
fmt.Println(fmt.Sprintf("msg.Message: %s", msg.Message))
fmt.Println(fmt.Sprintf("msg.SubscribedChannel: %s", msg.SubscribedChannel))
fmt.Println(fmt.Sprintf("msg.Timetoken: %d", msg.Timetoken))
case presence := <-listener.Presence:
fmt.Println(" --- PRESENCE: ")
fmt.Println(fmt.Sprintf("%s", presence))
}
}
}()
// End Init Listener
// Add Listener
pn.AddListener(listener)
// Subscribe
pn.Subscribe().
Channels([]string{"pubnub_onboarding_channel"}).
WithPresence(true).
Execute()
<-waitForConnect
// Publish
msg := map[string]interface{}{
"sender": pn.Config.UUID,
"content": "Hello From Go SDK",
}
resPub, statusPub, errPub := pn.Publish().
Channel("pubnub_onboarding_channel").
Message(msg).
Execute()
fmt.Println(resPub, statusPub, errPub)
// End Publish
// History
res, status, err := pn.History().
Channel("pubnub_onboarding_channel").
Count(10).
IncludeTimetoken(true).
Execute()
if res != nil {
fmt.Println(" --- HISTORY: ")
if res.Messages != nil {
for _, v := range res.Messages {
fmt.Println(fmt.Sprintf("Message: %s, Timetoken: %d", v.Message, v.Timetoken))
}
} else {
fmt.Println(fmt.Sprintf("Messages null"))
}
fmt.Println(fmt.Sprintf("EndTimetoken: %d", res.EndTimetoken))
fmt.Println(fmt.Sprintf("StartTimetoken: %d", res.StartTimetoken))
fmt.Println("")
} else {
fmt.Println(fmt.Sprintf("StatusResponse: %s %e", status.Error, err))
}
// End History
}