【发布时间】:2019-11-13 07:10:14
【问题描述】:
我正在学习如何使用Redis。
我特指learning the pub/sub 功能。
我将ioredis 与expressjs 和nodejs 一起使用。
我遇到过this code
var Redis = require("ioredis");
var redis = new Redis();
var pub = new Redis();
redis.subscribe("news", "music", function(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", function(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", function(channel, message) {
// Both `channel` and `message` are buffers.
});
现在我的问题是:
redis 是如何知道谁订阅了什么,以便向他们发送消息?每个 redis 客户端是否都有一个唯一的 id 来标识它们? (就像socket.io 的情况一样)
【问题讨论】:
-
我猜是使用整数的客户端 ID。当你在 redis-cli 中输入
CLIENT ID命令时得到的相同的数字