skip to Main Content

Im running a function to ingest data to a Redis instance. However, I am encountering an issue where I can’t seem to connect to the client.

Here is the part of the code where it connects to the server.

const redis = require('redis');
require('dotenv').config;

        const REDISHOST = process.env.REDISHOST;
        const REDISPORT = 6379;
        const REDISAUTH = process.env.AUTHSTRING;

        const client = redis.createClient({
            port: REDISPORT,
            host: REDISHOST,
            password: REDISAUTH
        });

        await client.connect();

Here is the whole error message:

"Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)
    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)"

Any help would be appreciated. Thanks

I have tried using ioredis instead of redis but it shows a different error altogether.

2

Answers


  1. It’s trying to connect to localhost and Redis doesn’t exist there. IF not given a hostname, this is the default behavior of Redis.

    Looking at your code, this is probably because the REDISHOST environment variable isn’t defined.

    Login or Signup to reply.
  2. The suggested solution to the problem is to modify the code to use a URL-based connection string instead of explicitly specifying the Redis server configuration parameters. The new connection string should be in the format of:

    redis[s]://[[username][:password]@][host][:port][/db-number]
    

    Change:

    const client = redis.createClient({
      port: REDISPORT,
      host: REDISHOST,
      password: REDISAUTH
    });
    

    To:

    createClient({
      url: 'redis://alice:[email protected]:6380'
    });
    

    That worked for me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search