0
const redis = require('redis');
const client = redis.createClient({
 host: 'redis-19606.redislabs.com',
 port: 19606,
 password: 'password'
});
client.on('ready', () => {
 console.log('redis is connected');
});
client.on('error', (err) => {
 console.log('redis is disconnected: ', err);
});
(async () => {
 try {
 await client.connect();
 } catch (error) {
 console.error('error while connecting redis', error);
 }
})();

This somehow does not seem to work. What am I doing wrong? It keeps connecting to 127.0.0.1:6379 which is the default config instead of what I am passing. This is only happening with nodejs client of redis. go-redis the golang client for redis is working flawlessly.

asked Dec 24, 2021 at 10:54
0

4 Answers 4

1

Just a guess, but which Node Redis version do you use? I had difficulties upgrading myself. The configuration of the client has changed since version 4.x.x. Since version 4.x.x you have to use a confiuration according to Client Configuration. Therefore use

const client = redis.createClient({
 socket: {
 host: 'redis-19606.redislabs.com',
 port: 19606,
 }
});

or use a URL

const client = redis.createClient({
 url: "redis://redis-19606.redislabs.com:19606"
});

Your client configuration matches the Node Redis version 3.x.x but not 4.x.x. Please see Redis NPM v3.1.2 and Redis NPM v4.0.1 for details.

answered Dec 24, 2021 at 13:14
Sign up to request clarification or add additional context in comments.

1 Comment

I wasn't aware of the socket option. The redis docs (docs.redis.com/latest/rs/references/client_references/…) is not right. Thank you for that!
1

you should maintain this format of Redis connecting string:

redis://:YOUR_PASSWORD@YOUR_ENDPOINT:YOUR_PORT
Tyler2P
2,37030 gold badges26 silver badges34 bronze badges
answered Apr 29, 2022 at 9:45

Comments

0
  1. What version of redis are you using?
  2. Do not explicitly conect to redis by writing "client.connect()" but instead use this after setting host, port and password:
    redisClient.on("connect", () => {})
    

and then you can use redisClient variable to get and set values.

  1. Also if you're trying to connect to a redislabs server then I think your host name might be incorrect and double check your host from redislabs.
answered Dec 24, 2021 at 13:06

Comments

0

Try this new Redis("redis://username:[email protected]:6380/4");

Check the documentation Link

answered May 20, 2022 at 21:43

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.