0

I´m using Firebase to get a list of messages. The problem is that I only need to see messages sent after I opened the page. Basically I don´t need to store the messages in the database I just need to broadcast it to all connected devices.

ref.limitToLast(1).on('child_added', function(snapshot) { console.log(snapshot.name(), snapshot.val()); }); 

The code above returns the last child correctly but I´m filling my database with messages I don´t need. When is the right time to delete "old" messages?

4
  • 1
    You could store the post date/time of the message in the records and only return those >= date/time user connected Commented Aug 18, 2017 at 4:32
  • what if there is one device that has been connected for a REALLY long time. I think you just have to figure out when it is acceptable to start hiding the old stuff. I feel like firebase should be able to hold a lot for you though. Commented Aug 18, 2017 at 4:34
  • that´s a good point. I´m afraid that device 1 receives and deletes the record and then device 2 gets nothing. not sure if Firebase broadcasts simultaneously. Commented Aug 18, 2017 at 4:40
  • Point (one of them anyway) of firebase that it is "live". so the changes that one device makes (like delete) can be handled as events for all of the users. Commented Aug 18, 2017 at 4:51

2 Answers 2

1

I don't recommend to delete the old messages, instead of it makes every user take care of reading only what is new.

If your data structure about the messages is working then you only need to add 1 attribute to your model. Each time a message is sent add the timestamp to it in an attribute.

Here is the doc how to use the ServerValue.TIMESTAMP.

Now every message node can be ordered by the timestamp.

"messages": { "chat_1": { "push_key_usually":{ "message": "hello", "owner": "maybe the UID or the email", "chat_key": "chat_1", "key": "push_key_usually", "timestamp": 981212312 } } } 

To complete this, you need to make every user set its own timestamp when they load the chat. This way you can fetch that value from the database and order your message using that timestamp. The complete order is to query the value first if it is null then is the first time, simply ask for every message or work using a custom timestamp (can be zero or current time minus some minutes) and set the value. If it is not null, then use that value for your query and after you get it, set the timestamp again.

"connections": { "user_UID": 12312412 } 

Now you can do something like this:

reference.child("messages/chat_1/push_key_usuarlly").orderByChild("timestamp").startAt(yourTimeStamp).once... 
Sign up to request clarification or add additional context in comments.

Comments

0

The Firebase Database is not a message-passing mechanism, it is a state synchronization mechanism. If you want you can emulate a message passing mechanism on the database, but you'll have to do the work for that.

One way to accomplish this is to write the message for each device that you want to send it to. That means that every device can simply delete each message that it reads and your database will never be bigger than the total number of unread message-devices.

Another way is to store each message only once, but then track what devices have read it. Once every device has read it, you can delete the message. You'll need to store the "what devices have read each message" and the "list of all devices" in the database. This approach may require less storage, but it will definitely be more complex in code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.