Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:21:09+00:00 2026-06-09T03:21:09+00:00

This is a rather involved question, but I’ll try to explain it as simply

  • 0

This is a rather involved question, but I’ll try to explain it as simply & concisely as I can…

I am using Firebase to build a multi-user, web-based game. I am keeping a list of every round in the game. At the end of a round, every user is presented with a “Start” button that they click when they are ready to begin the next round. The round begins when at least 50% of users have clicked “Start”.

I have a Firebase reference gameRef for the game, a reference roundListRef that represents the list of rounds, and a reference roundRef that represents the current round.

I have attached a child_added callback to roundListRef so that when a new round is added, it becomes everyone’s current round:

roundListRef.on('child_added', function(childSnapshot, prevChildName) {
    roundRef = childSnapshot.ref();
});

I can track newRoundVotes and activePlayers, and calculate 50% easily from there. If 50% is reached, a new round is added, which triggers everyone’s child_added event and the new round will begin from there…

gameRef.child('newRoundVotes').on('value', function(snapshot) {
    var newRoundVotes = snapshot.val();

    gameRef.child('activePlayers').once('value', function(snapshot) {
        var activePlayers = snapshot.val();

        if (newDriveVotes / activePlayers >= 0.5)
            addNewRound();
    });
});

My questions is, how do I ensure that only one new round is added, and that everyone is on the same round?

For example, say there are 10 players and 4 have already voted to start the next round. If the 6th player votes before his child_added event is triggered from the 5th player, then a round will also be added for the 6th player.

The problem is similar to .set() vs .transaction(), but no quite the same (from my understanding).

Does anyone have a solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:21:12+00:00Added an answer on June 9, 2026 at 3:21 am

    I think you can likely solve this with a transaction if the round names are known ahead of time. E.g. if you just use /round/0, /round/1, /round/2, etc.

    Then you could have some code like:

    function addNewRound() {
        var currentRound = Number(roundRef.name());
        var nextRound = currentRound + 1;
    
        // Use a transaction to try to create the next round.
        roundRefList.child(nextRound).transaction(function(newRoundValue) {
            if (newRoundValue == null) {
                // create new round.
                return { /* whatever should be stored for the round. */ };
            } else {
                // somebody else already created it.  Do nothing.
            }
        });
    }
    

    Does this work for your scenario?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is rather laughable, but I can't seem to get simple C++
This is a rather vague question but here we go - I would like
This is probably (hopefully) a pretty simple question, but I can't seem to get
My question is rather complex, but I thought I should give it a try.
Hi folks would really appreciate your help in answering this rather involved question. Please
This is a rather broad question, but I'm a beginner-intermediate programmer that has just
Ok, this might be a rather silly question, but what is the benefit of
This may be rather noobish but I'm gonna ask anyhow. I have a class
I got a simple question about jQuery but rather javascript approaches in general... Is
Have a rather abstract question for you all. I'm looking at getting involved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.