forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listeners.go
50 lines (41 loc) · 862 Bytes
/
listeners.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
package main
import (
"fmt"
pubnub "github.com/crimsonred/go/v5"
)
func main() {
config := pubnub.NewConfig()
config.SubscribeKey = "demo"
config.PublishKey = "demo"
pn := pubnub.NewPubNub(config)
listener := pubnub.NewListener()
done := make(chan bool)
go func() {
for {
select {
case status := <-listener.Status:
switch status.Category {
case pubnub.PNConnectedCategory:
done <- true
}
case message := <-listener.Message:
//Channel
fmt.Println(message.Channel)
//Subscription
fmt.Println(message.Subscription)
//Payload
fmt.Println(message.Message)
//Publisher ID
fmt.Println(message.Publisher)
//Timetoken
fmt.Println(message.Timetoken)
case <-listener.Presence:
}
}
}()
pn.AddListener(listener)
pn.Subscribe().
Channels([]string{"ch"}).
Execute()
<-done
}