Heroku Node.js RedisCloud Redis::CannotConnectError on localhost instead of REDISCLOUD_URL

Viewed 247

When i try to connect my Nodsjs application to RedisCloud on Heroku I am getting the following error

Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)

I have even tried to directly set the redis URL and port in the code to test it out as well. But still, it tried to connect to the localhost on Heroku instead of the RedisCloud URL.

const {Queue} = require('bullmq');
const Redis = require('ioredis');

const conn = new Redis(
      'redis://rediscloud:mueSEJFadzE9eVcjFei44444RIkNO@redis-15725.c9.us-east-1-4.ec2.cloud.redislabs.com:15725'

// Redis Server Connection Configuration
console.log('\n==================================================\n');
console.log(conn.options, process.env.REDISCLOUD_URL);

const defaultQueue = () => {
    // Initialize queue instance, by passing the queue-name & redis connection
    const queue = new Queue('default', {conn});
    return queue;
};
module.exports = defaultQueue;

Complete Dump of the Logs https://pastebin.com/N9awJYL9

2 Answers

set REDISCLOUD_URL on .env file as follows

REDISCLOUD_URL =redis://rediscloud:password@hostname:port

import * as Redis from 'ioredis'; export const redis = new Redis(process.env.REDISCLOUD_URL);

I just had a hard time trying to find out how to connect the solution below worked for me.

Edit---- Although I had been passed the parameters to connect to the Redis cloud, it connected to the local Redis installed in my machine. Sorry for that! I will leave my answer here, just in case anyone need to connect to local Redis.

let express = require('express');
var redis = require('ioredis');

pwd = 'your_pwd'
url = 'rediss://host'
port = '1234'
redisConfig = `${url}${pwd}${port}`
client = redis.createClient({ url: redisConfig })

client.on('connect', function() {
    console.log('-->> CONNECTED');
});
client.on("error", function(error) {
    console.error('ERRO DO REDIS', error);
});
Related