Reputation: 8993
When my SPA starts up I'd like to ask for a single event which gives me the state of a path in the database which represents a list. Something like:
let users;
const handler = (snap) => {};
firebase.ref('/foo').once('value')
.then(snap => users = snap)
.then(firebase.ref('/foo').on('child_changed', handler))
The goal being that I retrieve all the users immediately as part of a single network event. I then watch for any new child records and only get the new children when they come in (rather than the whole list).
Upvotes: 1
Views: 416
Reputation: 326
use on "child_added" event. to fetch new user.
update: To fetch old message first use on "value" event. In the callback function register listener for "child_added" and unlisten on "value". We are not using 'once' because when the chat is new and there are no messages 'once' will not be triggered. In order to get only new messages sort them using orderByChild method but you will need some kind of child like a timestamp of the message and then use last message's timestamp from previous array that you received from on "value". startAt(lastMessage.timestamp) should help
Upvotes: 1