ioredis icon indicating copy to clipboard operation
ioredis copied to clipboard

Unhandled error event: Error: write EPIPE, Unhandled error event: Error: write ECONNRESET

Open usmon-masudjonov opened this issue 1 year ago • 4 comments

Hello, I've been staying stuck for 2 hours.

const IORedis= require("ioredis");

const redis = new IOREDIS({ host: "localhost", port: 6379, db: 0, password: "1234", });

Above, you can see my code. Redis is running as a docker container.

usmon-masudjonov avatar Aug 20 '22 10:08 usmon-masudjonov

I've answered this on an earlier issue.

The right way to do this is to handle the error gracefully. The problem seems to be the IORedis client's behaviour of timed disconnect when inactive but it has an auto reconnect mechanism that will keep trying to reconnect.

Use Node.js Event Emitter to handle it. View all events with client.eventNames() or see the references below. One can simply log out the error, reconnecting and connect events like this to understand the behaviour described above that IORedis is doing in the background. Consequently, this also handles the irritating error message. Hope this helps!

import Redis from 'ioredis';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname, '../.env') });

const client = new Redis({
  port: Number(process.env.SESSION_STORE_PORT) || 6379,
  host: process.env.SESSION_STORE_HOSTNAME,
});

// Listen to 'error' events to the Redis connection
client.on('error', error => {
  if (error.code === 'ECONNRESET') {
    console.log('Connection to Redis Session Store timed out.');
  } else if (error.code === 'ECONNREFUSED') {
    console.log('Connection to Redis Session Store refused!');
  } else console.log(error);
});

// Listen to 'reconnecting' event to Redis
client.on('reconnecting', err => {
  if (client.status === 'reconnecting')
    console.log('Reconnecting to Redis Session Store...');
  else console.log('Error reconnecting to Redis Session Store.');
});

// Listen to the 'connect' event to Redis
client.on('connect', err => {
  if (!err) console.log('Connected to Redis Session Store!');
});

export default client;

References:

Originally posted on #1203

jayantasamaddar avatar Sep 19 '22 16:09 jayantasamaddar

Are you using docker compose?

Ba7er avatar Feb 28 '23 18:02 Ba7er

I got this error too when using docker compose

sam30606 avatar Aug 07 '23 02:08 sam30606

Check if your password matches up in your connection settings and Redis config

p0358 avatar Nov 18 '23 08:11 p0358