Glen
Glen

Reputation: 705

Node redis keep connection alive in client version 4.1.0

I am having an issue where i have a long running node socket server which uses redis to store data. The problem seems to be when the socket server isn't being used for a while and contacting redis, the connection to redis is dropped off.

I read that the following would work to keep the connection alive:

const redisClient = redis.createClient({
    url: process.env.REDIS_HOST
})

redisClient.on('connect', () => {
    const redisStream = redisClient.stream;
    redisStream.setKeepAlive(true, 30000);
})

However this doesn't work in redis version 4.1.0, i get this following error:

TypeError: Cannot read properties of undefined (reading 'setKeepAlive')

It seems the redis client doesn't have a method setKeepAlive

Any ideas? I can't find anything in the documentation about this. Is there another way this is done now, or would it be better if my node app hooks to the redis "end" event and re-connects itself?

Upvotes: 2

Views: 1204

Answers (1)

Sang
Sang

Reputation: 4464

set pingInterval in createClient()

createClient({
  pingInterval: 30000 // 30 s
})

https://github.com/redis/node-redis/blob/259e9b2e1f184d5e83413a73a88bda85de814ac0/docs/client-configuration.md#createclient-configuration

Upvotes: 1

Related Questions