Skip to content

Commit

Permalink
Better PubSub example
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jul 24, 2018
1 parent 480db94 commit 14aebde
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ func ExampleClient_Watch() {

func ExamplePubSub() {
pubsub := client.Subscribe("mychannel1")
defer pubsub.Close()

// Wait for confirmation that subscription is created before publishing anything.
_, err := pubsub.Receive()
Expand All @@ -339,8 +338,20 @@ func ExamplePubSub() {
panic(err)
}

msg := <-ch
fmt.Println(msg.Channel, msg.Payload)
time.AfterFunc(time.Second, func() {
// When pubsub is closed channel is closed too.
_ = pubsub.Close()
})

// Consume messages.
for {
msg, ok := <-ch
if !ok {
break
}
fmt.Println(msg.Channel, msg.Payload)
}

// Output: mychannel1 hello
}

Expand Down

0 comments on commit 14aebde

Please sign in to comment.