Home > Enterprise >  Unable to connect to remote redis host [nodeJS]
Unable to connect to remote redis host [nodeJS]

Time:12-10

const redis = require('redis');
require('dotenv').config();
console.log(process.env.redisHost, ':', process.env.redisPort);
const redisClient = redis.createClient({
    host: process.env.redisHost,
    port: process.env.redisPort,
    password: process.env.redisKey
});
redisClient.connect();
redisClient.on('error', err => console.log('Redis error: ', err.message));
redisClient.on('connect', () => console.log('Connected to redis server'));
module.exports = redisClient;

I tried this sample from redis docs but still I'm getting an error stating: Redis error: connect ECONNREFUSED 127.0.0.1:6379

I logged the environment host and port variables to the console and I got the remote host ipv4 address, but still the client is trying to connect to localhost instead of remote host (I purposely uninstalled redis from my local device to check if the client is working as it is supposed to). I also confirmed that the remote redis host is working perfectly.

I also tried different methods like : https://cloud.google.com/community/tutorials/nodejs-redis-on-appengine

redis.createClient(port, host, {auth_pass: password});

But still, I got the same error.

I am able to connect to the redis host via commandline:

redis-cli.exe -h XX.XX.XX.XXX -a Password
XX.XX.XX.XXX:6379> set name dhruv
OK
XX.XX.XX.XXX:6379> get name
"dhruv"
XX.XX.XX.XXX:6379> exit

I'm trying to use redis on nodejs for the first time, so don't have a proper idea but I think I am doing everything right. Any solution/workaround will be helpful :D

CodePudding user response:

It worked with this code:

const url = `redis://${process.env.redisHost}:${process.env.redisPort}`;
const redisClient = redis.createClient({
    url,
    password: process.env.redisKey
});
redisClient.connect();

CodePudding user response:

can you check if in the destination the port is reachable. it maybe the firewall block your access

  • Related