问题描述
我在 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
(非常感谢这个项目的开发者本人)