#Redis - The client is closed.

1 messages · Page 1 of 1 (latest)

wise swift
#

I want to save a token in the redis cache after user sign-in in my app.
The cache config.service.ts file:

export class CacheConfigService {
  constructor(private configService: ConfigService) {}

  get url(): string {
    console.log(this.configService.get<string>('redis.url'));
    return this.configService.get<string>('redis.url') as string;
  }
}

The cacheModule file: @Module({ imports: [ CacheModule.register<RedisClientOptions>({ isGlobal: true, // store: redisStore, url: 'redis://' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT, }), ], })

The user signin method:

  if (
    originalImplementation.emailPasswordSignInPOST === undefined
  ) {
    throw Error('Should never come here');
  }

  let response: any =
    await originalImplementation.emailPasswordSignInPOST(input);

  // if (response.status === 'OK') {
  const formFields: any = input.formFields;
  const inputObject: any = {};

  for (let index = 0; index < formFields.length; index++) {
    const element = formFields[index];
    inputObject[element.id] = element.value;
  }
  const { email } = inputObject;

  const user = await this.userService.findOneByEmail(email);
  const id = user?.id;

  const payload = { email, id };
  const secret = 'mysecret';
  const token = jwt.sign(payload, secret, {
    expiresIn: '2h',
  });
  response.token = token;

  const cacheKey = 'Token';
  await this.redisClient.set(cacheKey, token, {
    EX: 60 * 60 * 24,
  });

  // }

  return response;
},```
Please note that this is a different module.
When I send signin request from postman than it logs this error in the console: "Error: The client is closed"
covert chasm
#

Check this issue: https://github.com/dabroek/node-cache-manager-redis-store/issues/40, there's some discussion on which versions work together and how the client should be instantiated.
Other than that, are you sure that redis is running and accepting connections on the provided uri?
does running DEBUG='*' npm run start:dev produce some relevant logs?

wise swift
#

I have looked at it but the version is not the issue and I ran the command with npm run start dev DEBUG='*' but no error in the console and it runs completely fine and redis is also running perfectly fine.

#

I am using docker to run container. Does this have anything to do with that?

covert chasm
#

Did you expose the ports? Can you show the command which you used to run the redis container?

wise swift
#

I have a docker-compose file

covert chasm
#

And the DEBUG='*' part must be before the command. It enables debug output, which includes extra info like this:

  ioredis:redis status[127.0.0.1:6379]: connecting -> connect +228ms
  ioredis:redis status[127.0.0.1:6379]: connect -> ready +0ms
  ioredis:connection send 1 commands in offline queue +0ms
  ioredis:redis write command[127.0.0.1:6379]: 0 -> info([]) +0ms
  ioredis:redis status[127.0.0.1:6379]: connecting -> connect +1ms
  ioredis:redis status[127.0.0.1:6379]: connect -> ready +0ms
  ioredis:connection send 1 commands in offline queue +1ms

Let's see the compose file then

wise swift
#
  redis:
    container_name: redis
    image: 'redis:latest'
    environment:
      X_REDIS_PORT: ${REDIS_PORT}
    networks:
      app_network:
        ipv4_address: 172.26.0.12
    ports:
      - ${REDIS_PORT}:${REDIS_PORT}``` and I am exposing the port 6379
#

Then the command DEBUG='*' npm run start:dev is not working in my machine

covert chasm
#

It's one of the way of enabling the debug output, you basically want to set the DEBUG env variable to *.
https://github.com/debug-js/debug#windows-command-prompt-notes

For the compose file, can you try without the networks section? I'm not sure atm, but I think you'll need to include a host network too if you're running the app on your host (e.g. not in docker)

wise swift
#

Ok that works but that outputs a lots of data. I am not sure where to look

wise swift
covert chasm
#

If you got the debug enabled, search for redis. There should be something like I've sent above.

Just comment the networks lines and run docker-compose up -d to apply the changes. I'm assuming you're running the npm run directly on your system, not as a docker container.

services:
  redis:
    container_name: redis
    image: 'redis:latest'
    environment:
      X_REDIS_PORT: ${REDIS_PORT}
    #networks:
    #  app_network:
    #    ipv4_address: 172.26.0.12
    ports:
      - ${REDIS_PORT}:${REDIS_PORT}
wise swift
#

Surprisingly there is no information about redis in the console

#

Something is very wrong but I don't know what

covert chasm
#

I'd try other way of instantiating the store. Several posts mention await redisStore({...}) (with the await) — maybe that's the key.
I don't use it personally, so can't provide a working sample.