我在我的Nestjs项目上使用Redis。因此,我使用了svtslv/nestjs-ioredis
包。我对Nestjs(和Typescript)还不是很有经验。但我试图弄清楚如何获得第二个客户端连接(到同一数据库),因为我想与订阅者和发布者一起工作。
当使用这个nest包时,类似于在Nestjs中完成的next 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", (channel, message) => {
// Both `channel` and `message` are buffers.
});
RedisModule.forRoot({}, 'secondConnection')
@InjectRedis('secondConnection') private readonly redis: Redis
(非常感谢这个项目的开发者本人)