Miquel Las Heras
Miquel Las Heras

Reputation: 760

How to know when client stop listening for changes in firestore for specific reference?

I am struggling to find a solution for an interesting thing I need to do in Firestore.

Would be awesome if you help me. I will try to explain:

I have something like poker tables, where users can create tables and join them. When a user has a table opened other users can see him in the table. When he leaves the table he should disappear ( here is where I have the problem )

Leaving the table can happen by navigating to other page, by closing tab, etc. He can be in the same table in different tabs, browsers or devices ( i can change this if there is no other option ).

So what I need is to display which players are connected to a table. Can you think of an optimal solution? How would you implement this?

.

If it helps, some things I tried but don't work:

Upvotes: 0

Views: 150

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

So what I need is to display which players are connected to a table. Can you think of an optimal solution? How would you implement this?

The most simple solution I can think of is to create a collection of tables and store in each document an array of user ids:

Firestore-root
   |
   --- tables (collection)
         |
         --- tableId (document)
               |
               --- users: ["usersIdOne", "usersIdTwo", "usersIdThree"]

Now to check wich players are connected to a table, you can simply attach a realtime listener on a document/query so you can get data in realtime. This means that once a player joins or leaves a table, the listener will fire and you'll be notified instantly.

To add a user to a table, simply add its uid in the users array and to remove a listener, remove its uid from the users array. For more informations:

Please see arrayUnion and arrayRemove.

Upvotes: 1

Related Questions