Reputation: 2315
I was having some problem when trying to do a real-time refresh from listening to changes in Firebase using JavaScript. What I tried to do is a chat system. Upon starting a new chat with another user, I will load all the chats between the two users.
code
My Firebase query:
code
The problem is the code above does not work in 'real-time'. Real-time as in when user A send to B, without manually refreshing the page to reload the chats, the user B should be able to seethe latest message from user A.
The code above only work if the user B manually refresh the page to query from Firebase again.
Is there something like polling from JavaScript to Firebase to check for changes?
Thanks!
Upvotes: 0
Views: 1614
Reputation: 900
use on value
event to automatically get new arrived msg.
Problem is on value
will return all message value instead of new one.
you need to store timestamp along with msg data,
ref.on('value', function(snapshot){
//request data from firebase that having message-timestamp greater than last message-timestamp in previous `on-value` event
});`
To get specific msg, you need to use firebase query
var ref= firebase.database().ref("users/msgtimestamp");
var query = ref.orderByChild('msgtimestamp'); //write query-logic to get only new message
query.on('value', function(snapshot) {
});
I hope my answer will help you
Upvotes: 1