MCPcopy
hub / github.com/redis/go-redis / ExamplePubSub_Receive

Function ExamplePubSub_Receive

example_test.go:562–590  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

560}
561
562func ExamplePubSub_Receive() {
563 pubsub := rdb.Subscribe(ctx, "mychannel2")
564 defer pubsub.Close()
565
566 for i := 0; i < 2; i++ {
567 // ReceiveTimeout is a low level API. Use ReceiveMessage instead.
568 msgi, err := pubsub.ReceiveTimeout(ctx, time.Second)
569 if err != nil {
570 break
571 }
572
573 switch msg := msgi.(type) {
574 case *redis.Subscription:
575 fmt.Println("subscribed to", msg.Channel)
576
577 _, err := rdb.Publish(ctx, "mychannel2", "hello").Result()
578 if err != nil {
579 panic(err)
580 }
581 case *redis.Message:
582 fmt.Println("received", msg.Payload, "from", msg.Channel)
583 default:
584 panic("unreached")
585 }
586 }
587
588 // sent message to 1 rdb
589 // received hello from mychannel2
590}
591
592func ExampleScript() {
593 IncrByXX := redis.NewScript(`

Callers

nothing calls this directly

Calls 5

ReceiveTimeoutMethod · 0.80
SubscribeMethod · 0.65
CloseMethod · 0.65
ResultMethod · 0.65
PublishMethod · 0.65

Tested by

no test coverage detected