javascript - only new records using firebase -
i´m using firebase list of messages. problem need see messages sent after opened page. don´t need store messages in database need broadcast connected devices.
ref.limittolast(1).on('child_added', function(snapshot) { console.log(snapshot.name(), snapshot.val()); });
the code above returns last child correctly i´m filling database messages don´t need. when right time delete "old" messages?
i don't recommend delete old messages, instead of makes every user take care of reading new.
if data structure messages working need add 1 attribute model. each time message sent add timestamp in attribute.
here doc how use servervalue.timestamp.
now every message node can ordered timestamp.
"messages": { "chat_1": { "push_key_usually":{ "message": "hello", "owner": "maybe uid or email", "chat_key": "chat_1", "key": "push_key_usually", "timestamp": 981212312 } } }
to complete this, need make every user set own timestamp when load chat. way can fetch value database , order message using timestamp. complete order query value first if null first time, ask every message or work using custom timestamp (can 0 or current time minus minutes) , set value. if not null, use value query , after it, set timestamp again.
"connections": { "user_uid": 12312412 }
now can this:
reference.child("messages/chat_1/push_key_usuarlly").orderbychild("timestamp").startat(yourtimestamp).once...
Comments
Post a Comment