node-redis: Getting the ClientClosedError when using quit() / disconnect() methods

Hi. I’ve noticed that calling the quit() or disconnect() methods throws the following error:

ClientClosedError: The client is closed
    at RedisSocket.disconnect (/Users/peter/Playground/test-redis/node_modules/@node-redis/client/dist/lib/client/socket.js:62:19)
    at Commander.disconnect (/Users/peter/Playground/test-redis/node_modules/@node-redis/client/dist/lib/client/index.js:279:64)
    at stop (/Users/peter/Playground/test-redis/index.js:6:16)
    at Object.<anonymous> (/Users/peter/Playground/test-redis/index.js:9:1)

I am running the following script:

const redis = require('redis');

const client = redis.createClient();

async function stop() {
  await client.disconnect();
}

stop();

I am using:

  • Node 16.2
  • redis-server 5.0.7
  • node-redis 4.0.0
  • Mac OS Monterey 12.0.1

I am not sure if this behaviour is valid, since it prevents me from shutting down the connection gracefully.

I did not have this problem previously when using the 3.1.2 version of node-redis.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20

Most upvoted comments

You should call .quit/.disconnect only after you have called .connect

const redis = require('redis');

async function run() {
  const client = redis.createClient();

  await client.connect();

  console.log(client.isOpen); // this is true

  await client.disconnect();
}

run();

@rajiff I don’t understand what is your issue exactly. You can store Redis connection in a singleton instance and reuse it in several modules.

Here’s a very simple example of Redis connection:

// redis-connection.js
import redis from 'redis';

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

  async connect({ host, port, password }) {
    this.Client = redis.createClient({ host, port, password });
    await this.Client.connect();
    return this.Client;
  }
}

export default new RedisClient();

Create connection somewhere in your application during your application launch:

// launcher.js
import connection from './redis-connection.js';

async function launch() {
  await connection.connect({ host, port, password });
}

launch();

You can then access the connected client from other modules:

// write-value.js
import connection from './redis-connection.js';

async function storeValue() {
  await connection.Client.set('key', 'value');
}

storeValue();
// read-value.js
import connection from './redis-connection.js';

async function readValue() {
  const key = await connection.Client.get('key');
  console.log(key);
}

readValue();

This helps me to create connection ,set and get keys from redis 6.2.6

@rajiff I don’t understand what is your issue exactly. You can store Redis connection in a singleton instance and reuse it in several modules.

Here’s a very simple example of Redis connection:

// redis-connection.js
import redis from 'redis';

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

  async connect({ host, port, password }) {
    this.Client = redis.createClient({ host, port, password });
    await this.Client.connect();
    return this.Client;
  }
}

export default new RedisClient();

Create connection somewhere in your application during your application launch:

// launcher.js
import connection from './redis-connection.js';

async function launch() {
  await connection.connect({ host, port, password });
}

launch();

You can then access the connected client from other modules:

// write-value.js
import connection from './redis-connection.js';

async function storeValue() {
  await connection.Client.set('key', 'value');
}

storeValue();
// read-value.js
import connection from './redis-connection.js';

async function readValue() {
  const key = await connection.Client.get('key');
  console.log(key);
}

readValue();

ununtu server 20.04 Error: The client is closed at Commander._RedisClient_sendCommand (/home/major/pool/node_modules/@redis/client/dist/lib/client/index.js:493:31) at Commander.commandsExecutor (/home/major/pool/node_modules/@redis/client/dist/lib/client/index.js:188:154) at BaseClass.<computed> [as info] (/home/major/pool/node_modules/@redis/client/dist/lib/commander.js:8:29) at checkRedisVersion (/home/major/pool/init.js:147:15)

I’m so tired I can’t find the mistake 😦

Hey @siyavashhamdi thanks for posting it

Yes that should work, after posting question here, I did below and it worked for me, was about to update here too

const redis = require('redis');

async function createRedisClient () {
  const client = redis.createClient();

  client.on('connect', () => console.log('Connected to REDIS!'));
  client.on('error', (err) => console.log('Error connecting to REDIS: ', err));

  await client.connect();

  return client;
}

module.exports = createRedisClient();

and to use it

const redisClient = require("./reidsClient");

redisClient.then((client) => { 
  // do something with the client
  await client.set("Hello", "Await World");
  // this also works, if we don't want to use await
  client.set("Hello", "Real World").then(()=> { 
     // can do something here 
  });
});

That changed in V4