简体   繁体   English

在 Firebase 中管理聊天频道的最佳方式

[英]Best way to manage Chat channels in Firebase

In my main page I have a list of users and i'd like to choose and open a channel to chat with one of them.在我的主页中,我有一个用户列表,我想选择并打开一个频道与其中一个聊天。

I am thinking if use the id is the best way and control an access of a channel like USERID1-USERID2.我在想是否使用 id 是最好的方法并控制对像 USERID1-USERID2 这样的通道的访问。

But of course, user 2 can open the same channel too, so I'd like to find something more easy to control.但是当然,用户 2 也可以打开相同的频道,所以我想找到更容易控制的东西。

Please, if you want to help me, give me an example in javascript using a firebase url/array.如果你想帮助我,请给我一个使用 firebase url/array 的 javascript 示例。

Thank you!谢谢!

A common way to handle such 1:1 chat rooms is to generate the room URL based on the user ids.处理这种 1:1 聊天室的常用方法是根据用户 ID 生成聊天室 URL。 As you already mention, a problem with this is that either user can initiate the chat and in both cases they should end up in the same room.正如您已经提到的,一个问题是任何一个用户都可以发起聊天,在这两种情况下,他们都应该在同一个房间里结束。

You can solve this by ordering the user ids lexicographically in the compound key.您可以通过在复合键中按字典顺序对用户 ID 进行排序来解决此问题。 For example with user names, instead of ids:例如使用用户名,而不是 id:

 var user1 = "Frank"; // UID of user 1 var user2 = "Eusthace"; // UID of user 2 var roomName = 'chat_'+(user1<user2 ? user1+'_'+user2 : user2+'_'+user1); console.log(user1+', '+user2+' => '+ roomName); user1 = "Eusthace"; user2 = "Frank"; var roomName = 'chat_'+(user1<user2 ? user1+'_'+user2 : user2+'_'+user1); console.log(user1+', '+user2+' => '+ roomName);
 <script src="https://getfirebug.com/firebug-lite-debug.js"></script>

A common follow-up questions seems to be how to show a list of chat rooms for the current user.一个常见的后续问题似乎是如何显示当前用户的聊天室列表。 The above code does not address that.上面的代码没有解决这个问题。 As is common in NoSQL databases, you need to augment your data model to allow this use-case.正如在 NoSQL 数据库中常见的那样,您需要扩充您的数据模型以允许此用例。 If you want to show a list of chat rooms for the current user, you should model your data to allow that.如果您想显示当前用户的聊天室列表,您应该对数据进行建模以允许这样做。 The easiest way to do this is to add a list of chat rooms for each user to the data model:最简单的方法是将每个用户的聊天室列表添加到数据模型中:

"userChatrooms" : {
  "Frank" : {
    "Eusthace_Frank": true
  },
  "Eusthace" : {
    "Eusthace_Frank": true
  }
}

If you're worried about the length of the keys, you can consider using a hash codes of the combined UIDs instead of the full UIDs.如果您担心键的长度,您可以考虑使用组合 UID 的哈希码而不是完整 UID。

In a typical database schema each Channel / ChatGroup has its own node with unique $key (created by Firebase).在典型的数据库模式中,每个频道/聊天组都有自己的节点,带有唯一的 $key(由 Firebase 创建)。 It shouldn't matter which user opened the channel first but once the node (& corresponding $key) is created, you can just use that as channel id.哪个用户首先打开频道无关紧要,但是一旦创建了节点(和相应的 $key),您就可以将其用作频道 ID。

Hashing / MD5 strategy of course is other way to do it but then you also have to store that "route" info as well as $key on the same node - which is duplication IMO (unless Im missing something).散列/ MD5 策略当然是另一种方法,但是您还必须在同一节点上存储该“路由”信息以及 $key - 这是重复的 IMO(除非我遗漏了一些东西)。

Hashing with js-sha256 module worked for me with directions of Frank van Puffelen and Eduard.在 Frank van Puffelen 和 Eduard 的指导下,使用 js-sha256 模块进行散列对我有用。

import SHA256 from 'crypto-js/sha256'
let agentId = 312
let userId = 567
let chatHash = SHA256('agent:' + agentId + '_user:' + userId)

We decided on hashing users uid's, which means you can look up any existing conversation,if you know the other persons uid.我们决定散列用户的 uid,这意味着你可以查找任何现有的对话,如果你知道其他人的 uid。

Each conversation also stores a list of the uids for their security rules, so even if you can guess the hash, you are protected.每个对话还存储其安全规则的 uid 列表,因此即使您能猜出哈希值,您也会受到保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM