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
You should call
.quit/.disconnectonly after you have called.connectThis 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:
Create connection somewhere in your application during your application launch:
You can then access the connected client from other modules:
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
and to use it
That changed in V4