Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
not able to connect to redis in nodejs

First thing first, the details:

node version: v16

Using redislabs cloud (v6.2.3)

npm package redis version 4.0.3

Here's the code...

const redis = require("redis");
require("dotenv").config();

const client = redis.createClient({
  host: process.env.REDIS_URI,
  port: process.env.REDIS_PORT,
  password: process.env.REDIS_PASSWORD
});

client.on("connect", () => {
  console.log("Connected to our redis instance!");
  client.set("iAmAKey", "Value");
});

On running it doesn't output anything :( and just quits simply after some time.

Any help will be appreciated. Thanks!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Well, I made it work now. Adding this as a reference for any future wanderer with the same issue.

So the community suggested to use URL instead.

const redis = require("redis");
require("dotenv").config();

(async () => {
  const client = redis.createClient({
    url: `redis://default:${process.env.REDIS_PASSWORD}@${process.env.REDIS_URI}:${process.env.REDIS_PORT}`,
  });

  client.on("error", (err) => console.log("Redis Client Error", err));

  await client.connect();

  await client.set("key", "value");
  console.log("Redis Connected!")
  const value = await client.get("key");
  console.log(value);
})();

Here's the issue for future reference: Not able to connect to redis-labs cloud redis server #1892

about 4 years ago · Juan Pablo Isaza Denunciar

0

The call to .createClient creates a client but doesn't actually connect to Redis. So the connect event never fires. Node Redis 4.x supports promises so you really don't need the callback at all. You can accomplish the same thing with:

const redis = require("redis");
const client = redis.createClient();

await client.connect();
await client.set('foo', 'bar');
let foo = client.get('foo');

Note that I removed the usage of dotenv and just have it connect to Redis on localhost in my sample code for brevity.

about 4 years ago · Juan Pablo Isaza Denunciar

0

additionnel information to @Amresh this is what worked for me:

  url: `redis://${process.env.REDIS_PASSWORD}@${process.env.REDIS_URI}:${process.env.REDIS_PORT}`,

without default word.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda