使用 redis 构建与 socket.io 和 NodeJs 的实时聊天 [英] Use redis to build a real time chat with socket.io and NodeJs

查看:58
本文介绍了使用 redis 构建与 socket.io 和 NodeJs 的实时聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to built a real time chat system for my project but actually I have some problems with Redis because I want my data stored as better as possible.

我的问题:

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

I'd like to use Socket Io to do real time chatting in a closed group (of two people), but how to store messages?

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

Redis is a key value store and that means that if i want to store something i need to add an unique key to my data before getting stored.

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

If the same user posts more than one messages which keys would I use inside redis? I'm thinking about unique ids as unique keys but since I want to be able to fetch this comments when a user log the chat page, but if I do that I need to write another database that relate chat ids to the user that posted that message

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

Am I forgetting anything? Is there a best method to do this?

抱歉我的英语不好.

推荐答案

Redis 不仅仅是键值存储.

Redis is more then key-value store.

所以你想要以下内容:

  • 聊天消息,
  • 两人讨论,
  • 您没有提到时间限制,所以假设您在一段时间后归档邮件,
  • 您也不会说是否要在两个人之间建立单独的话题",例如论坛或连续消息,例如 facebook.我假设是连续的.

对于每个用户,您必须存储他发送的消息.假设 APP_NAMESPACE:MESSAGES::.我们在此处添加 userId 以便我们可以轻松检索单个用户发送的所有消息.

For each user, you have to store messages he sends. Let's say APP_NAMESPACE:MESSAGES:<USER_ID>:<MESSAGE_ID>. We add userId here so that we can easily retreive all messages sent by a single user.

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

And, for each two users, you need to track their conversations. As a key, you can simply use their userids APP_NAMESPACE:CONVERSATIONS:<USER1_ID>-<USER2_ID>. To make sure you always get the same, shared conversation for the two users, you can sort their ids alfabetically, so that users 132 and 145 will both have 132:145 as conversation key

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

So what to store in "conversations"? Let's use a list: [messageKey, messageKey, messageKey].

好的,但是现在的 messageKey 是什么?上面的 userId 和 messageId 的组合(这样我们就可以得到实际的消息).

Ok, but what is now the messageKey? Combo of userId above and a messageId (so we can get the actual message).

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

So basically, you need two things:

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

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

With node and standard redis/hiredis client this would be somehting like (I'll skip the obvious error etc checks, and I'll write ES6. If you cannot read ES6 yet, just paste it to 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);
    });
  });
}

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

Now that's crude and untested, but that's the gist of how you can do this.

这篇关于使用 redis 构建与 socket.io 和 NodeJs 的实时聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆