Vikas Sharma
Vikas Sharma

Reputation: 1

Redis is working with Python but not with nodeJS

I have below code in Python3 which is working well

import redis

sentinel_host = '10.3.0.151'
sentinel_port = 26379

sentinel = redis.Redis(host=sentinel_host, port=sentinel_port)
masters = sentinel.sentinel_masters()

for master_name, master_info in masters.items():
    print("Master Name:", master_name)
    print("Master Info:", master_info)

    slaves = sentinel.sentinel_slaves(master_name)
    for slave_info in slaves:
        print("Slave Info:", slave_info)

The same kind of code when I run in nodeJS it does not work

const Redis = require("ioredis");

const sentinelHost = "10.3.0.151";
const sentinelPort = 26379;

const sentinel = new Redis({
  sentinels: [{ host: sentinelHost, port: sentinelPort }],
  name: "mymaster",
});

async function printMasterAndSlaves() {
  //console.log(sentinel);
  sentinel.set("mykey", "value"); // Returns a promise which resolves to "OK" when the command succeed$
  sentinel.get("mykey", (err, result) => {
    console.log("Reacgwduhwdhwuhdwudh");
    if (err) {
      console.log(err);
    } else {
      console.log(result); // Prints "value"
    }
  });

  try {
    const masters = await sentinel.sentinel("masters");
    for (const masterInfo of masters) {
      console.log("Master Name:", masterInfo.name);
      console.log("Master Info:", masterInfo);

      const slaves = await sentinel.sentinel("slaves", masterInfo.name);
      for (const slaveInfo of slaves) {
        console.log("Slave Info:", slaveInfo);
      }
    }
  } catch (error) {
    console.error("Error:", error);
  } finally {
    sentinel.quit();
  }
}

printMasterAndSlaves();

**Python code Output: **Master Name: mymaster Master Info: {'name': 'mymaster', 'ip': '127.0.0.1', 'port': 6379, 'runid': '6deb03bcc6393b652cbed25eb6c4b1d8ccde0fec', 'flags': 'master', 'link-pending-commands': '0', 'link-refcount': '1', 'last-ping-sent': 0, 'last-ok-ping-reply': 253, 'last-ping-reply': 253, 'down-after-milliseconds': 30000, 'info-refresh': 4465, 'role-reported': 'master', 'role-reported-time': 251167207, 'config-epoch': 0, 'num-slaves': 0, 'num-other-sentinels': 0, 'quorum': 2, 'failover-timeout': 180000, 'parallel-syncs': 1, 'is_master': True, 'is_slave': False, 'is_sdown': False, 'is_odown': False, 'is_sentinel': False, 'is_disconnected': False, 'is_master_down': False} **NodeJS code output: **[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) [ioredis] Unhandled error event: Error: All sentinels are unreachable. Retrying from scratch after 10ms. at connectToNext (/home/ubuntu/exomind-dashboard/node_modules/ioredis/built/connectors/SentinelConnector/index.js:64:31) at SentinelConnector.connect (/home/ubuntu/exomind-dashboard/node_modules/ioredis/built/connectors/SentinelConnector/index.js:120:16) at /home/ubuntu/exomind-dashboard/node_modules/ioredis/built/Redis.js:121:64 at new Promise () at EventEmitter.connect (/home/ubuntu/exomind-dashboard/node_modules/ioredis/built/Redis.js:103:25) at Timeout._onTimeout (/home/ubuntu/exomind-dashboard/node_modules/ioredis/built/redis/event_handler.js:171:18) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7) [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379

I tried multiple other libraries and passing few params nothing helped. I tried various other libraries like ioredis,redis-senitel but nothing has worked out for me

Upvotes: 0

Views: 167

Answers (1)

Vikas Sharma
Vikas Sharma

Reputation: 1

Got solved using below code

const redis = require("redis");
const createRedisClient = () => {
  const client = redis.createClient({
    url: `redis://${REDIS_HOST}:${REDIS_PORT}`,
  });

  client.on("connect", () => appLogger.info("Connected to REDIS!"));
  client.on("error", (err) =>
    errorLogger.error("Error connecting to REDIS: ", err)
  );
  client.connect();

  return client;
};
module.exports = createRedisClient();

Upvotes: 0

Related Questions