redis pub / sub模型用于select性推送的通知系统

我正在寻找build立一个通知系统像Facebook,我的应用程序逻辑(发布者)将推动所有用户生成的通知到redis系统。

User 1 --------------> Redis (Channel : notifications) User 2 --------------> Redis (Channel : notifications) User 3 --------------> Redis (Channel : notifications) 

如上所示,用户1,2,3生成的所有活动都发送到相同的通道通知。

我有一个监听redis的node.js /socket.io服务器作为这些通知的订阅者。 (订阅频道通知

现在, 我如何有select地只推送某些通知给某些订阅者? 就像Facebook通知一样,我只会收到发送给我的私人邮件的通知,而不会收到发送给他人的邮件的通知。

感谢您的帮助提前。

为每个用户创build个人频道,例如notifications.User1,notifications.User2,…,并让每个用户订阅他/她的频道。 (你不需要担心频道的大小)

如果用户共享一个Redis连接,则每当连接收到任何订阅消息时,都可能需要从通道名称中识别出接收方用户。

更新:

我假设这种情况:

当用户login您的应用程序时,您的nodejs应用程序可能会知道用户的ID。
然后,您的应用程序仅为用户订阅频道,例如,如下所示:
(这是一种伪代码,我不确定nodejs。)

 onUserLoggedIn(string userId) { ... string userChannel = "notifications.user." + userId; // If userId == "yash", // then userChannel == "notifications.user.yash" redisConnection.command("subscribe", userChannel); ... } 

当你的连接从你的redis服务器收到一条发布的消息时:

 onMessagePublished(string channel, string message) { ... // You can get userId from channel id. vector<string> tokens = parseTokensFromChannel(channel); // If channel == "notifications.user.yash", // tokens == {"notifications", "user", "yash"}; if (tokens[0] == "notifications") { if (tokens[1] == "user") { ... string userId = tokens[2]; onMessagePublishedForUser(userId, message); ... } else { ... } ... } else { ... } ... } onMessagePublishedForUser(string userId, string message) { // You can handle the message for each user. // I don't think your user may need it's own handling code per user. ... } 

在这种情况下,根本不需要任何硬编码。
您的redis连接可以通过发送命令“订阅”来订阅任何redis频道。
我假设你的用户将自定义可识别的用户信息(至less是用户的id)发送到nodejs服务器,这样你的nodejs应用程序可以使通道名称变成dynamic的。
(如果你的用户不会发送用户的ID,你怎么识别每个用户?)