lundi 17 juin 2019

Synchronising data with Firestore on users connected via Web

My set up:

Users connected to fire-base via WEB.

I am trying to figure out a way to let a user know that a database document has been edited. How do i achieve this ?

I am trying to build a matchmaking system but i am unsure how to utilize fire-base functions and fire-store to achieve this.

User A starts matchmaking and finds there are no documents available with any players, so they start by creating a document in 'matches' collection and this document has a total of 1 players, this happens through a transaction to avoid any concurrency issues(a separate problem). User A then waits for another user to join.

User B tries to matchmake and finds a document in 'matches' collection and this document has enough room (max 2 players) so User B can join, the response in the HTTP request sends a response to User B with the document ID and the other user Id in the room.

My problem here is I don't know how to notify User A about the new User that has joined the room.

Between, I cant use Web Push as it is unsupported on IOS and other browser versions.

Ive tried Web Push, it does not work. And i am using the Fire-base Web SDK.

  db.runTransaction(async t=>{
    // console.log('S T A R T - T R A N S A C T I O N ', vUser);
    const dref = await db.collection('matches').where('totalPlayers', '==', 1).get();
    if (dref.size > 0) {
    const d = await t.get(dref.docs[0].ref);
    // console.log(d.data());
      let doc = d;
      // console.log('G O T - D O C  for ', vUser);
      let data = doc.data();
      if (data.totalPlayers + 1 > 2) {
        return Promise.reject('too many players');
      }
      else {
        data.totalPlayers+=1;
        data.players.push(vUser);
        // console.log({_id:doc.id});
        let returnData = {_id:doc.id, data:data};
        t.update(doc.ref, data);
        return Promise.resolve(returnData);
      }
    }
    else {
      let docData = {
        'players': [vUser], totalPlayers: 1,
          created: admin.firestore.Timestamp.now()._seconds
      };
      let matchRef =  db.collection('matches').doc();
      t.set(matchRef, docData);
return Promise.resolve(docData);
      });

    }
  }).then((result)=>{
    console.log('transaction S U C C E S S ', vUser, result);
    response.status(200).send(result);
  }).catch(err=>{
    console.log('transaction F A I L ', vUser, err);
  })
}```




Aucun commentaire:

Enregistrer un commentaire