Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hapi catbox not able to connect to redis server

First of all, I would like to say that I'm new to Hapi so don't judge me hard.

I am following this tutorial trying to set up server-side caching based on Redis using catbox and catbox-redis npm packages, and I get the following error:

{
  reason: Error: Connection is closed.
      at Redis.connectionCloseHandler (/home/yuriy/dev/sources/all/hapi-getting-started/node_modules/ioredis/built/redis/index.js:305:24)
      at Object.onceWrapper (events.js:300:26)
      at Redis.emit (events.js:210:5)
      at processTicksAndRejections (internal/process/task_queues.js:75:11)
}

As you can see it says that error is in ioredis (v4.14.1 according to package-lock.json) package which is dependency of catbox-redis.

I have Redis server running locally.

username@my-computer:~$ redis-cli -v
redis-cli 4.0.9
username@my-computer:~$ redis-cli 
127.0.0.1:6379> ping
PONG

Here is my package.json:

{
  "name": "hapi-getting-started",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.ts",
  "scripts": {
    "build": "rimraf dist && tsc",
    "start": "rimraf dist && tsc && node dist/index.js",
    "dev": "tsc -w | nodemon dist/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@hapi/catbox": "^10.2.3",
    "@hapi/catbox-redis": "^5.0.5",
    "@hapi/hapi": "^18.4.0",
    "rimraf": "^3.0.0",
    "typescript": "^3.7.2"
  },
  "devDependencies": {
    "@types/hapi__catbox": "^10.2.2",
    "@types/hapi__catbox-redis": "^5.0.0",
    "@types/hapi__hapi": "^18.2.6",
    "@types/node": "^12.12.14",
    "nodemon": "^2.0.1"
  }
}

And here is my src/index.ts:

const Hapi = require('@hapi/hapi');
const CatboxRedis = require('@hapi/catbox-redis');

console.log(`Running environment ${process.env.NODE_ENV || 'dev'}`);

// Catch uncaught exceptions
process.on('uncaughtException', (error: Error) => {
  console.error(`uncaughtException ${error.message}`);
  console.error({ reason });
});

// Catch unhandled rejected promises
process.on('unhandledRejection', (reason: any) => {
  console.error(`unhandledRejection ${reason}`);
  console.error({ error });
});

const init = async () => {
  const server = Hapi.server({
    host: 'localhost',
    port: 8000,
    cache: {
      name: 'redis-cache',
      provider: {
        constructor: CatboxRedis,
        options: {
          partition: 'my_cached_data',
          tls: {},
        },
      },
    },
  });

  await server.start();
  console.log('Server running on %s', server.info.uri);
};

init();

Any ideas what I am doing wrong? I've spent a lot of time on this issue so any help would be very appreciated.

like image 950
Yuriy Rypka Avatar asked Oct 17 '25 13:10

Yuriy Rypka


2 Answers

Okay, it appears that there is an issue in Hapi official docs on caching (Server-side Caching section). The solution was very simple but not obvious: I just removed tls: {},.

  const server = Hapi.server({
    host: 'localhost',
    port: 8000,
    cache: {
      name: 'redis-cache',
      provider: {
        constructor: CatboxRedis,
        options: {
          partition: 'my_cached_data',
          // tls: {}, <-- Here is a problem, remove this line
        },
      },
    },
  });

This is ioredis config params. From catbox-redis docs:

tls - an object representing TLS config options for ioredis.

You can find more details in ioredis docs.

like image 167
Yuriy Rypka Avatar answered Oct 20 '25 04:10

Yuriy Rypka


Okay, it appears that there is an issue in Hapi official docs on caching (Server-side Caching section). The solution was very simple but not obvious: I just removed tls: {},.

  const server = Hapi.server({
    host: 'localhost',
    port: 8000,
    cache: {
      name: 'redis-cache',
      provider: {
        constructor: CatboxRedis,
        options: {
          partition: 'my_cached_data',
          // tls: {}, <-- Here is a problem, remove this line
        },
      },
    },
  });

This is ioredis config params. From catbox-redis docs:

tls - an object representing TLS config options for ioredis.

You can find more details in ioredis docs.

like image 23
Yuriy Rypka Avatar answered Oct 20 '25 02:10

Yuriy Rypka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!