首页 > 解决方案 > 如何获得额外的客户端连接 (nestjs-ioredis)

问题描述

我在我的 Nestjs 项目中使用 Redis。因此,我正在使用包svtslv/nestjs-ioredis

我在 Nestjs(和 Typescript)方面还不是很有经验。但我试图弄清楚如何获得第二个客户端连接(到同一个数据库),因为我想与订阅者和发布者一起工作。

使用此 Nest 包时,在 Nestjs 中完成的下一个节点实现类似:

const Redis = require("ioredis");
const redis = new Redis();
const pub = new Redis();
redis.subscribe("news", "music", (err, count) => {
  // Now we are subscribed to both the 'news' and 'music' channels.
  // `count` represents the number of channels we are currently subscribed to.

  pub.publish("news", "Hello world!");
  pub.publish("music", "Hello again!");
});

redis.on("message", (channel, message) => {
  // Receive message Hello world! from channel news
  // Receive message Hello again! from channel music
  console.log("Receive message %s from channel %s", message, channel);
});

// There's also an event called 'messageBuffer', which is the same as 'message' except
// it returns buffers instead of strings.
redis.on("messageBuffer", (channel, message) => {
  // Both `channel` and `message` are buffers.
});

标签: redisnestjsioredis

解决方案


RedisModule.forRoot({}, 'secondConnection')
@InjectRedis('secondConnection') private readonly redis: Redis

(非常感谢这个项目的开发者本人)


推荐阅读