Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- pubnub "github.com/pubnub/go"
- )
- func main() {
- config := pubnub.NewConfig()
- // config.PublishKey = "my_publish_key"
- config.SubscribeKey = "my_subscribe_key"
- pn := pubnub.NewPubNub(config)
- listener := pubnub.NewListener()
- waitForConnect := make(chan bool)
- waitForMsg := 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))
- waitForMsg <- true
- case presence := <-listener.Presence:
- fmt.Println(" --- PRESENCE: ")
- fmt.Println(fmt.Sprintf("%s", presence))
- }
- }
- }()
- pn.AddListener(listener)
- pn.Subscribe().
- Channels([]string{"pubnub_onboarding_channel"}).
- Execute()
- <-waitForConnect
- fmt.Println("Connected")
- <-waitForMsg
- fmt.Println("Get Message")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement