In my Node.js application I had lot of
Error: Socket closed unexpectedly
That caused my application to crash and restart multiple times because of the unhandled error.
This happens because the server cuts the connection when it is idle for a given time (in my server it was 5 minutes)
If you are ok with this behaviour and you just want your application not to crash you can just add a listener on the error event. like this:
import * as redis from "redis";
async redisClientFactory() {
const redisClient = client.createClient({
url: process.env.redis_connection_string,
});
redisClient.on("error", (err) => console.error("client err", err));
await redisClient.connect();
return redisClient;
}
I took the solution from this post on Stack Overflow
If you want your application to keep the connection you can just ping your server every 10 second (or the time you want) adding a pingInterval to the options
import * as redis from "redis";
async redisClientFactory() {
const redisClient = client.createClient({
url: process.env.redis_connection_string,
pingInterval: 10000,
});
redisClient.on("error", (err) => console.error("client err", err));
await redisClient.connect();
return redisClient;
}
Motorcycle rider
American football player
DIY enthusiast
Web developer on free time