11
rated 0 times
[
18]
[
7]
/ answers: 1 / hits: 7972
/ 2 Years ago, thu, may 5, 2022, 12:00:00
I have a microservice in NestJs, here is the main.ts file:
async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
transport: Transport.REDIS,
options: {
url: 'redis://localhost:6379',
}
})
await app.listen();
console.log('Redis is running');
}
bootstrap();
I want to make calls to redis db from the service:
import { createClient } from '@redis/client';
async createWorkspaceShareCode(data: GetWorkspaceShareCodeMessage) {
const client = createClient({
url: 'redis://localhost:6379'
});
await client.connect();
await client.set('key', 'value');
const value = await client.get('key');
return value;
}
But that's not what I want, here are the issues:
- every time I make request, it creates new connection
- I need to write this code in each function to connect to redis (even though I'm already connected in main.ts file)
Here is the question: how can I use some kind of RedisService in my NestJs microservice?
I have already tried installing redis and nestjs-redis libraries, but the first one creates the issue described above and the second one just throws me an error: https://github.com/skunight/nestjs-redis/issues/97
More From » node.js