7
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

asked Dec 8, 2021 at 11:36

5 Answers 5

16

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();
answered Dec 10, 2021 at 7:24
Sign up to request clarification or add additional context in comments.

Comments

1

The configuration object changed in redis v4, you can view the new options here. Below is how I've configured it in my project.

const redisClient = redis.createClient({ socket: { port: process.env.redisPort, host: process.env.redisHost } })
redisClient.connect()
answered May 25, 2023 at 13:19

Comments

0

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

answered Dec 8, 2021 at 12:28

10 Comments

yes it is, I am even able to use it remotely from my device. But the problem is that the redis client is not even trying to connect to the remote host. It is trying to connect to 127.0.0.1 instead of remote ip address
usualy redis get ip from config file , please change the redis configuration from config file installdir/redis/etc/redis. conf
another suggestion try to use docker for redis connection
What field in particular should I search in the redis conf file? Can you help me with that? like is it cluster-ip? Also, for now I don't want to use docker as I am having a problem with my database in docker, so I was looking for a already deployed version of redis
try this way redis.js.org github.com/redis/node-redis in this website you can find how to connect without config file and how you put the information
|
0

If you want to connect to a remote redis db in an express nodejs app, you need to pass the url like this:

const url = `rediss://default:your_password@your_host:your_port`;
const client = redis.createClient({
 url,
});

note that your host should start with something like private-db-redis-nyc3... or db-redis..., if you find redis:// don't include it.

answered Aug 20, 2023 at 9:59

Comments

0

I was trying to connect to a redis instance running on localhost with the url localhost:port but what made it work for me was using 127.0.0.1:port instead

answered Dec 15, 2023 at 8:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.