1

I am new to golang. Trying to to use redis channel subscription for multiple subscribers. I am using below code in main(). Just wanted to know that is it a good approach to use Redis Client for multiple subscriber. Is there any better approach? can I create Redis Client in another go file and initialize that in main() method

redisClient := redis.NewClient(&redis.Options{
    Addr: os.Getenv("REDIS_ENDPOINT") + ":6379",
    DB:   0,
})

go func() {
    subscriber1 := redisClient.Subscribe("channel1")
    for {
        msg, err := subscriber1.ReceiveMessage()
        go func() {
            gofile1.listenSubscriber1(msg, err) // this method is present in another go file
        }()
    }
}()
go func() {
    subscriber2 := redisClient.Subscribe("channel2")
    for {
        msg, err := subscriber2.ReceiveMessage()
        go func() {
            gofile2.listenSubscriber2(msg, err) // // this method is present in another go file
        }()
    }
}()
ppb
  • 2,299
  • 4
  • 43
  • 75

0 Answers0