Firebase聊天 - 删除旧的消息 [英] Firebase chat - removing old messages

查看:119
本文介绍了Firebase聊天 - 删除旧的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个房间聊天,私人信息,温和​​和现在的一切,这一切都很好!
在测试聊天的过程中,我意识到聊天中输入的所有消息都会保存起来,如果您有很多人使用聊天,那么您的Firebase会占用相当多的空间。



为了给你一个我正在寻找的例子,让我告诉你我是如何处理私人信息的:

<当John向Bob发送私人消息时,消息将被添加到John和Bobs的私人消息列表中,如下所示:

  / private / Bob<  - 这里附加的消息

这是一个例子,说明如何在聊天中使用2条消息查看Firebase,以及2条私人消息:

 chat:{
516993ddeea4f:{
string:这里有一些消息,
time:Sat 13th - 18 :20:29,
username:test,
},
516993e241d1c:{
string:another messa在这里,
时间:星期六13:18:20:34,
用户名:Test2,
}
},
private:{
test:{
516993f1dff57:{
string:测试PM回复!,
time:Sat 13th - 18:20:49,
username:Test2,
},
516993eb4ec59:{
string:Test private message!,
time:Sat 13th - 18:20:43,
username:test,
}
},
Test2:{
516993f26dbe4:{
string:测试PM回复!,
time:星期六13:18:20:50,
username :Test2,
},
516993eab8a55:{
string:测试私人信息!,
time:Sat 13th - 18:20 :42,
username:test,
}
}
}
}

也是如此。现在,如果鲍勃在那里断开连接,鲍勃私人消息列表被删除,但约翰仍然能够看到他与鲍勃的谈话,因为他得到了他的名单上的所有消息的副本。如果John在Bob之后断开连接,则Firebase将被清除并且不再存储对话。

有没有办法在常规聊天中实现类似的功能?
向所有使用聊天的用户发送消息似乎不是一个好的解决方案(?)。或者是有可能以某种方式让Firebase只保留最新的100条消息?例如,

我希望它是有道理的!

亲切的问候

非常感谢Firebase团队的所有帮助,我非常感激。

有几种不同的方法解决这个,比其他一些更复杂的实施。最简单的解决方案是让每个用户只读最近的100条消息:



  var messagesRef = new Firebase(https://< example> .firebaseio.com / message).limit(100); 
messagesRef.on(child_added,function(snap){
// render new chat message。
});
messagesRef.on(child_removed,function(snap){
//可选择从DOM
中删除此聊天信息//如果您只希望显示最后100条信息
} );

您的邮件列表仍将包含所有邮件,但不会影响性能,因为每个客户端只会询问为最后的100条消息。另外,如果要清理旧数据,则最好将每条消息的优先级设置为消息发送的时间戳。然后,删除所有超过2天的消息:

var timestamp = new Date( );
timestamp.setDate(timestamp.getDate() - 2);
messagesRef.endAt(timestamp).on(child_added,function(snap){
snap.ref()。remove();
});

希望这有助于您!


I have create a chat with just 1 room, private messages, moderation and everything now and it all works great! While I was testing the chat, I realised that all the messages every typed in the chat got saved and if you had a lot of people using the chat it would very quickly take up quite a lot of space in your Firebase.

To give you an example of what I am looking for, let me show you how I handle private messages:

When John sends a private message to Bob, the message will be added to both John and Bobs list of private messages, like this:

/private/John <-- Message appended here
/private/Bob <-- Message appended here

This is an example of how the firebase would look with 2 message in the chat, and 2 private messages:

{
  "chat" : {
    "516993ddeea4f" : {
      "string" : "Some message here",
      "time" : "Sat 13th - 18:20:29",
      "username" : "test",
    },
    "516993e241d1c" : {
      "string" : "another message here",
      "time" : "Sat 13th - 18:20:34",
      "username" : "Test2",
    }
  },
  "private" : {
    "test" : {
      "516993f1dff57" : {
        "string" : "Test PM reply!",
        "time" : "Sat 13th - 18:20:49",
        "username" : "Test2",
      },
      "516993eb4ec59" : {
        "string" : "Test private message!",
        "time" : "Sat 13th - 18:20:43",
        "username" : "test",
      }
    },
    "Test2" : {
      "516993f26dbe4" : {
        "string" : "Test PM reply!",
        "time" : "Sat 13th - 18:20:50",
        "username" : "Test2",
      },
      "516993eab8a55" : {
        "string" : "Test private message!",
        "time" : "Sat 13th - 18:20:42",
        "username" : "test",
      }
    }
  }
}

and the same goes the other way around. Now if Bob where to disconnect, Bobs list of private messages get removed, but John is still able to see his conversation with Bob because he got a copy of all the messages on his list. If John then disconnect after Bob, the Firebase would be cleaned and their conversation no longer stored.

Is there a way to achieve something like this with the General chat? Pushing a message to all the users who are using the chat does not seem like a good solution (?). Or is it possible to somehow make the Firebase only keep the latest 100 messages for example?

I hope it makes sense!

Kind regards

Ps. Big thanks for the Firebase team for all the help so far, I really appreciate it.

解决方案

There are a few different ways to tackle this, some more complicated to implement than others. The simplest solution would be have each user only read the latest 100 messages:

var messagesRef = new Firebase("https://<example>.firebaseio.com/message").limit(100);
messagesRef.on("child_added", function(snap) {
  // render new chat message.
});
messagesRef.on("child_removed", function(snap) {
  // optionally remove this chat message from DOM
  // if you only want last 100 messages displayed.
});

Your messages list will still contain all messages but it won't affect performance since every client is only asking for the last 100 messages. In addition, if you want to clean up old data, it is best to set the priority for each message as the timestamp of when the message was sent. Then, you remove all the messages older than 2 days with:

var timestamp = new Date();
timestamp.setDate(timestamp.getDate()-2);
messagesRef.endAt(timestamp).on("child_added", function(snap) {
  snap.ref().remove();
}); 

Hope this helps!

这篇关于Firebase聊天 - 删除旧的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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