NodeJS can not connect to Redis
Asked Answered
D

4

6

I have nodejs app which needs to connect to Redis database. However, when trying to connect to Redis it always returns "false". I have running Redis instance on the same machine which is accessible from cli, also have another nodejs app which is connected to this same instance of Redis.

I don'g see anything wrong in /var/log/redis/redis-server.log nor printed in the console.

Any ideas what might be the issue?

$ nodejs -v v8.11.1

$redis-server -v Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=1bc80a08306a3efd

const redis = require('redis'),
redisClient = redis.createClient(6379, '127.0.0.1');

const REDIS_USER_DATA_INDEX = 2;

redisClient.select(REDIS_USER_DATA_INDEX);

redisClient.on('connect', function () {
    console.log('redis connected');
}).on('error', function (error) {
    console.log(error);
});

console.log(`connected ${redisClient.connected}`);

EDIT I'm not getting any events in both redisClient.on() callbacks

Doriandoric answered 15/5, 2018 at 8:10 Comment(2)
Does your code display "redis connected" if you wait a few seconds ?Willamina
No, nothing happens on onConnect or onError events.Doriandoric
A
11

redis' connect() function is an async function. The promise has to resolve for you to be able to use the connection.

The code below should work:

const redis = require("redis");

let redisPort = 6379;  // Replace with your redis port
let redisHost = "127.0.0.1";  // Replace with your redis host
const client = redis.createClient({
    socket: {
      port: redisPort,
      host: redisHost,
    }
  });

(async () => {
    // Connect to redis server
    await client.connect();
})();


console.log("Attempting to connect to redis");
client.on('connect', () => {
    console.log('Connected!');
});

// Log any error that may occur to the console
client.on("error", (err) => {
    console.log(`Error:${err}`);
});

// Close the connection when there is an interrupt sent from keyboard
process.on('SIGINT', () => {
    client.quit();
    console.log('redis client quit');
});
Albano answered 19/4, 2022 at 20:6 Comment(0)
I
6

Basically, you're calling redisClient.on(), which is an Async function, and you're outputting the connected status outside the callback function which definitely returns false since redis is still not connected. I would suggest put your code within the callback function. Try the code given below:

    const redis = require('redis'),
redisClient = redis.createClient(6379, '127.0.0.1');

const REDIS_USER_DATA_INDEX = 2;

redisClient.select(REDIS_USER_DATA_INDEX);

redisClient.on('connect', function () {
    console.log('redis connected');
    console.log(`connected ${redisClient.connected}`);
}).on('error', function (error) {
    console.log(error);
});

Let me know if you've any question. Thanks

Infrasonic answered 15/5, 2018 at 8:25 Comment(3)
Did you installed the Redis package??Infrasonic
If you want, I can share the node app. Let me know your thoughtsInfrasonic
Not sure what was the problem but after restart I am able to connect now. Thanks for your help!Doriandoric
D
2

Adding client.connect() solves my issue.

client = redis.createClient();

client.connect()
Damning answered 13/6, 2022 at 6:7 Comment(0)
W
0

Maybe it's because when you are trying to display redisClient.connected, you are not connected yet (Javascript is Asynchrone)

I think you should log this directly inside your event like this :

redisClient.on('connect', function () {
    console.log('redis connected');
    console.log(`connected ${redisClient.connected}`);
}).on('error', function (error) {
    console.log(error);
});
Willamina answered 15/5, 2018 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.