微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获得额外的客户端连接 (nestjs-ioredis)

如何解决如何获得额外的客户端连接 (nestjs-ioredis)

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

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

当使用这个 nest 包时,类似于在 nestjs 中完成的下一个 Node 实现:

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",message) => {
  // Both `channel` and `message` are buffers.
});

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。