Search code examples
node.jsredispublish-subscribenode-redisnodejs-server

Redis Nodejs Pub/Sub is not working as expected for different selected db


I am using Nodejs and "redis" for a pub sub to hear for expired Events.

In default its working just fine but when I switch to a different selected db it just does not get subscribe but I am seeing the setex working properly

Here is my code:-

const redis = require("redis");

const pub = redis.createClient({ url: process.env.REDIS_URL });
pub.connect();
pub.configSet("notify-keyspace-events", "Ex");
pub.select(4).then(() => {
   pub.setEx('mm', 5, "lopl")

})


const sub = pub.duplicate();
sub.connect()
sub.select(4).then(() => {
   sub.subscribe("__keyevent@0__:expired", (key) => {
      console.log("key=> ", key)
   })
})

Solution

  • I got the answer, sub.subscribe("keyevent@0:expired)

    in this line that 0 describe the db number change it to your desired and you are good to go