Jason Axelrod
Jason Axelrod

Reputation: 7805

Live Syncing of Google Firebase Without Node.JS?

I am trying to use Google Firebase to keep a web page sync'd between browser clients. It's a very simple page, with only two text fields:

    <input class="firebase" id="text1">
    <input class="firebase" id="text2">
    <input type="button" value="test" onclick="setData()">

Firebase seems to be all set up properly. When the submit button is clicked, it does the following:

    function setData()
    {
        docData = [];

        $('.firebase').each(function (index, value)
        {
            docData[$(this).attr('id')] = $(this).val();
        });

        console.log(docData);
        db.collection("test").doc("12345").set(Object.assign({}, docData));
    }

This so far is working great, I can see the data getting updated in real time on my Firebase console.

However, I would like it so that when the firebase document is updated, the text fields on the page, for anyone else who is viewing the page, automatically get updated with the new values.

I don't have Node.JS installed on my website, and I wouldn't even know where to begin on getting it installed.

Is there something simple I can do to keep these fields sync'd between clients?

Upvotes: 0

Views: 223

Answers (1)

Jack
Jack

Reputation: 5614

If you want live update, just use the onSnapshot method:

db.collection("cities").doc("SF")
    .onSnapshot(function(doc) {
        console.log("Current data: ", doc.data());
    });

For more details, refer to the documentation.

Upvotes: 2

Related Questions