I’d like to implement a stack (queue) that many users can push() to and many can pop() from in a FILO manner.
Is there an equivalent to pop() method to retrieve/remove the last item of the list?
For example:
var popRef = firebaseRef.pop();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is very doable, but a tad tricky. Since you’ll have multiple users trying to remove items, you have to deal with the case of multiple users trying to remove the same item (this is really a distributed systems problem). Presumably you only want one user to succeed. The solution there is to use a transaction to ensure that only one user can successfully remove a particular item.
We have a “work queue” example on github that’s very similar to what you’re looking for: https://github.com/firebase/firebase-work-queue
The big difference between it and what you asked for is that it’s FIFO instead of FILO. To change it to be FILO, you’ll want to modify this line from workqueue.js:
The .startAt() there tells it to grab the first item from the beginning of the list (i.e. the oldest item). To get the last item in the list (i.e. the newest item), you can change “startAt()” to “endAt()” or just omit “startAt()” entirely (by default we’ll get the last item).