【发布时间】: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.
});
【问题讨论】: