使用 redis 与 socket.io 和 NodeJs 建立实时聊天

Use redis to build a real time chat with socket.io and NodeJs

我想为我的项目构建一个实时聊天系统,但实际上我在使用 Redis 时遇到了一些问题,因为我希望我的数据存储得尽可能好。

我的问题:

我想用Socket Io在一个封闭的群(两个人)中进行实时聊天,但是如何存储消息?

Redis 是一个键值存储,这意味着如果我想存储一些东西,我需要在存储之前为我的数据添加一个唯一的键。

如果同一用户发布多条消息,我会在 redis 中使用哪些键?我正在考虑将唯一 ID 作为唯一键,但因为我希望能够在用户登录聊天页面时获取此评论,但如果我这样做,我需要编写另一个数据库,将聊天 ID 与发布该内容的用户相关联留言

我是不是忘记了什么?有没有最好的方法来做到这一点?

抱歉我的英语不好。

redis 是你项目中的一个约束吗?

你可以通过这个http://autobahn.ws/python/wamp/programming.html

Redis 不仅仅是 key-value 存储。

所以你想要以下内容:

  • 聊天消息,
  • two-person 次讨论,
  • 你没有提到时间限制,所以让我们假设你在一段时间后归档消息,
  • 你也不要说你是否想在两个人之间分开 "threads",比如论坛或连续消息,比如 facebook。我假设是连续的。

对于每个用户,您必须存储他发送的消息。假设 APP_NAMESPACE:MESSAGES:<USER_ID>:<MESSAGE_ID>。我们在这里添加 userId 以便我们可以轻松地检索单个用户发送的所有消息。

并且,对于每两个用户,您需要跟踪他们的对话。作为密钥,您可以简单地使用他们的用户 ID APP_NAMESPACE:CONVERSATIONS:<USER1_ID>-<USER2_ID>。为确保两个用户始终获得相同的共享对话,您可以按字母顺序对他们的 ID 进行排序,这样用户 132 和 145 都将 132:145 作为对话键

那么要在 "conversations" 中存储什么?让我们使用一个列表:[messageKey, messageKey, messageKey].

好的,但是现在的 messageKey 是什么?上面的 userId 和 messageId 的组合(所以我们可以获得实际的消息)。

所以基本上,您需要两件事:

  1. 存储消息并为其指定一个 ID
  2. 将对此消息的引用存储到相关对话中。

对于节点和标准 redis/hiredis 客户端,这有点像(我将跳过明显的错误等检查,我将编写 ES6。如果您还不能阅读 ES6,只需将其粘贴到 babel):

 // assuming the init connects to redis and exports a redisClient
import redisClient from './redis-init';
import uuid from `node-uuid`;


export function storeMessage(userId, toUserId, message) {

  return new Promise(function(resolve, reject) {

    // give it an id.
    let messageId = uuid.v4(); // gets us a random uid.
    let messageKey = `${userId}:${messageId}`;
    let key = `MY_APP:MESSAGES:${messageKey}`;
    client.hmset(key, [
      "message", message,
      "timestamp", new Date(),
      "toUserId", toUserId
    ], function(err) {
      if (err) { return reject(err); }

      // Now we stored the message. But we also want to store a reference to the messageKey
      let convoKey = `MY_APP:CONVERSATIONS:${userId}-${toUserId}`; 
      client.lpush(convoKey, messageKey, function(err) {
        if (err) { return reject(err); }
        return resolve();
      });
    });
  });
}

// We also need to retreive the messages for the users.

export function getConversation(userId, otherUserId, page = 1, limit = 10) {
  return new Promise(function(resolve, reject) {
    let [userId1, userId2] = [userId, otherUserId].sort();
    let convoKey = `MY_APP:CONVERSATIONS:${userId1}-${userId2}`;
    // lets sort out paging stuff. 
    let start = (page - 1) * limit; // we're zero-based here.
    let stop = page * limit - 1;
    client.lrange(convoKey, start, stop, function(err, messageKeys) {

      if (err) { return reject(err); }
      // we have message keys, now get all messages.
      let keys = messageKeys.map(key => `MY_APP:MESSAGES:${key}`);
      let promises = keys.map(key => getMessage(key));
      Promise.all(promises)
      .then(function(messages) {
         // now we have them. We can sort them too
         return resolve(messages.sort((m1, m2) => m1.timestamp - m2.timestamp));
      })
      .catch(reject);
    }); 
  });
}

// we also need the getMessage here as a promise. We could also have used some Promisify implementation but hey.
export function getMessage(key) {
  return new Promise(function(resolve, reject)  {
    client.hgetall(key, function(err, message) {
      if (err) { return reject(err); }
      resolve(message);
    });
  });
}

这很粗糙且未经测试,但这就是您如何做到这一点的要点。