func()

in get-started-with-redis/pubsub.go [65:92]


func (team *Team) subscribe() error {
	ctx := context.Background()
	// Subscribe to the "challenge" channel
	pubSub := team.client.Subscribe(ctx, pubsubChan)

	// The first Subscribe() call creates the channel.
	// Until that point, any attempt to publish something fails.
	reply, err := pubSub.Receive(ctx)
	if err != nil {
		return fmt.Errorf("subscribing to channel '%s' failed: %w", pubsubChan, err)
	}
	// Expected response type is "*Subscription". Otherwise, something failed.
	switch reply.(type) {
	case *redis.Subscription:
		// Success!
	case *redis.Message:
		// The channel is already active and contains messages, hence also a success
	case *redis.Pong:
		// letL's call it a success
	default:
		return fmt.Errorf("subscribing to a channel failed: received a reply of type %T, expected: *redis.Subscription", reply)
	}

	team.channel = pubSub

	fmt.Printf("%s subscribed to channel '%s'\n", team.name, pubsubChan)
	return nil
}