【问题标题】:How to get an extra client connection (nestjs-ioredis)如何获得额外的客户端连接(nestjs-ioredis)
【发布时间】:2021-01-19 03:54:10
【问题描述】:

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

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

当使用这个 Nest-package 时,类似于在 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.
});

【问题讨论】:

    标签: redis nestjs ioredis


    【解决方案1】:
    RedisModule.forRoot({}, 'secondConnection')
    @InjectRedis('secondConnection') private readonly redis: Redis
    

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

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 2021-01-19
      • 2018-04-29
      • 1970-01-01
      • 2020-02-03
      • 2012-06-19
      • 2021-07-22
      • 1970-01-01
      • 2017-10-31
      相关资源
      最近更新 更多