0

I have the following RedisClient class that connects to a redis server:

import { promisify } from 'util';
import { createClient } from 'redis';

class RedisClient {
  constructor() {
    this.client = null;
  }

  async initialize() {
    this.client = RedisClient.getRedisClient();
    await this.waitForReady();
  }

  static getRedisClient() {
    return createClient();
  }

  async waitForReady() {
    return new Promise((resolve, reject) => {
      this.client.on('error', (err) => console.log(`Redis: ${err}`, reject));
      this.client.on('ready', () => console.log('Redis connected'), resolve);
    });
  }

  isAlive() {
    return this.client.isReady;
  }
}

const redisClient = new RedisClient();

(async () => {
  await redisClient.initialize();
})();

export default redisClient;

I want the redis client to be ready before I used it that is why i created the waitForReady function to await the creation of the redis client. I tested it with this code:

import redisClient from './utils/redis';

(async () => {
  console.log(redisClient.isAlive());
})();

But I am getting an undefined output to the console log. Why is happening I have tried to make the creation of the redis client asynchronous but still I am getting undefined. Is it because the isReady is deprecated or should I use the isOpen instead?

Update I am using redis 2.8.0

1
  • "(async () => { await redisClient.initialize(); })();" is pointless, that launches an anonymous task but does not wait for it. Same as just writing redisClient.initialize();. It is still exported before the client is done initialising. You would need to use top-level await in the module to achieve that. Commented Dec 1, 2023 at 19:31

2 Answers 2

0

Looks like you are using Node Redis. When using Node Redis, you must connect to the client after creating it.

Like this:

await this.client.connect()

There is newer syntax for this in the README which I haven't tried yet but that looks pretty slick:

import { createClient } from 'redis';

const client = await createClient()
  .on('error', err => console.log('Redis Client Error', err))
  .connect();

await client.set('key', 'value');
const value = await client.get('key');
await client.disconnect();
Sign up to request clarification or add additional context in comments.

1 Comment

I am not using the new version of redis, this way is in the newest version of redis (4.6.11). I am using redis (2.8.0) @GuyRoyse
0

Checking out at the node-redis examples documentation, it looks like isReady is still valid. You could use isOpen or isReady to check the redis client connection.

// isOpen will return True here as the client's socket is open now.
// isReady will return True here, client is ready to use.
console.log(`client.isOpen: ${client.isOpen}, client.isReady: ${client.isReady}`);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.