Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active December 20, 2015 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katowulf/6115099 to your computer and use it in GitHub Desktop.
Save katowulf/6115099 to your computer and use it in GitHub Desktop.
Using a simple update counter to maintain precise ordering of records in Firebase
var sequenceRef = new Firebase(...+'/entries');
var updateCounter = new Firebase(...+'/counter');
updateCounter.transaction(function(currentValue) {
return currentValue+1;
}, function(error, committed, snap) {
if( error ) { ... }
else if( committed ) {
var updateCounter = snap.val();
sequenceRef.push().setWithPriority({
updateCounter: updateCounter
userId: "user123",
timestamp: Firebase.ServerValue.TIMESTAMP
}, updateCounter);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment