mirror of https://github.com/go-redis/redis.git
Better PubSub example
This commit is contained in:
parent
480db94d33
commit
14aebdec92
|
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue