forked from mirror/redis
Better PubSub example
This commit is contained in:
parent
480db94d33
commit
14aebdec92
|
@ -322,7 +322,6 @@ func ExampleClient_Watch() {
|
||||||
|
|
||||||
func ExamplePubSub() {
|
func ExamplePubSub() {
|
||||||
pubsub := client.Subscribe("mychannel1")
|
pubsub := client.Subscribe("mychannel1")
|
||||||
defer pubsub.Close()
|
|
||||||
|
|
||||||
// Wait for confirmation that subscription is created before publishing anything.
|
// Wait for confirmation that subscription is created before publishing anything.
|
||||||
_, err := pubsub.Receive()
|
_, err := pubsub.Receive()
|
||||||
|
@ -339,8 +338,20 @@ func ExamplePubSub() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := <-ch
|
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)
|
fmt.Println(msg.Channel, msg.Payload)
|
||||||
|
}
|
||||||
|
|
||||||
// Output: mychannel1 hello
|
// Output: mychannel1 hello
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue