3

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

Harsha M V
  • 54,075
  • 125
  • 354
  • 529

3 Answers3

0

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);

0

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);
});
Alex DS
  • 81
  • 8
  • Do you know why this solved the error? Is there a difference between the createClient function and the Redis constructor function used by OP? – ryanwebjackson Apr 01 '22 at 00:39
  • Sorry Ray, this worked but I realized later that it was connected to my local Redis, although I had been passed the arguments to connect to the cloud. I will adit my answer. – Alex DS Apr 03 '22 at 23:27
0

Just wanted to post my case in case someone has the same problem like me. In my situation I was trying to use Redis with Bull, so i need it the url/port,host data to make this happened.

Here is the info: https://devcenter.heroku.com/articles/node-redis-workers

but basically you can start your worker like this:

let REDIS_URL = process.env.REDISCLOUD_URL || 'redis://127.0.0.1:6379';

//Once you got Redis info ready, create your task queue
const queue = new Queue('new-queue', REDIS_URL);

In the case you are using local, meaning 'redis://127.0.0.1:6379' remember to run redis-server: https://redis.io/docs/getting-started/

Wilson Muñoz
  • 191
  • 5
  • 7