0
const redis = require("redis");

let client = redis.createClient({
    host: process.env.host,
    port: process.env.port,
    password:process.env.password
});

(async () => {
    client.on('error', (err) => console.log('Redis Client Error', err));
    await client.connect();
    console.log("connected to redis")
  })();

I have added redis-heroku addon to my project, Now I am trying to access it from my code but its giving me this error: "AuthError: ERR Client sent AUTH, but no password is set".

Also when I am trying to connect from terminal, I am able to connect to it but when I type any redis command , I get this "Error: Connection reset by peer".

If I am using this on my localsystem and local redis server its working fine

it will be helpful if anyone can provide me a working code of heroku redis, I think redis has two urls: REDIS_URL, REDIS_TLS_URL. The problem might be arising because of this tls(more secure)

Kinldy help me Thanks

2 Answers2

0

Heroku redis does not expose a host, port, and password variables. Instead they expose a REDIS_URL that contains all of those things in one string.

I believe you need to call createClient like this...

createClient({
  url: process.env.REDIS_URL
});
TerribleDev
  • 2,195
  • 1
  • 19
  • 31
  • 1
    Redis Client Error Error: connect ECONNREFUSED at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1142:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: 'ip', port: 10080 } – Kanav Raina Dec 28 '21 at 16:09
  • Thanks for replying. I am getting this error with your code @TerribleDev – Kanav Raina Dec 28 '21 at 16:10
  • @KanavRaina I actually think this answer looks better. https://stackoverflow.com/a/65563689/3671357 – TerribleDev Dec 29 '21 at 00:25
  • const redis = require("redis"); const redisUrl = process.env.REDIS_TLS_URL ? process.env.REDIS_TLS_URL : process.env.REDIS_URL; const redisDefaults = { tls: { // Heroku uses self-signed certificate, which will cause error in connection, unless check is disabled rejectUnauthorized: false, }, }; const client = redis.createClient(redisUrl, redisDefaults); (async () => { client.on('error', (err) => console.log('Redis Client Error', err)); await client.connect(); console.log(`connected to redis ${redisUrl}`) })(); – Kanav Raina Dec 29 '21 at 04:43
  • I have already checked that answer as well, When I try this code this is getting connected to my local redis not heroku redis, Kinldy check this on your local system, if you are able to connect to heroku-redis, I am not able to connect to heroku-redis – Kanav Raina Dec 29 '21 at 04:45
0

In node-redis v4 the host and port should be inside a socket object, not directly on the main config object (see https://github.com/redis/node-redis/blob/master/docs/client-configuration.md):

const client = redis.createClient({
    socket: {
        host: process.env.host,
        port: process.env.port
    },
    password: process.env.password
});
Leibale Eidelman
  • 2,772
  • 1
  • 17
  • 28
  • const redis = require("redis"); const client = redis.createClient({ socket: { host: process.env.host, port: process.env.port }, password: process.env.password }); (async () => { client.on('error', (err) => console.log('Redis Client Error', err)); await client.connect(); console.log(`connected to redis `) })(); – Kanav Raina Dec 29 '21 at 04:29
  • I am getting this error connect ECONNREFUSED 34.228.251.67:10080 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1142:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '34.228.251.67', port: 10080 } – Kanav Raina Dec 29 '21 at 04:29