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!
4
Answers
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.
Here's the issue for future reference: Not able to connect to redis-labs cloud redis server #1892
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:Note that I removed the usage of
dotenv
and just have it connect to Redis onlocalhost
in my sample code for brevity.additionnel information to @Amresh this is what worked for me:
without default word.
I faced the same issue in Node 14 using Redis
^4.3.1
. Changing the version to^3.1.2
worked for me.