You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: firestore-counter/POSTINSTALL.md
+45-47Lines changed: 45 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -4,29 +4,28 @@ Before you can use this extension, you'll need to update your security rules, se
4
4
5
5
#### Update security rules
6
6
7
-
Update your Cloud Firestore security rules to allow reads and writes to the `_counter_shards_` subcollection where you want the extension to count, for example:
7
+
Update your Cloud Firestore security rules to allow lookups and writes to the `_counter_shards_` subcollection where you want the extension to count. For example, to allow clients to increment the `visits` field on any document in the `pages` collection, you can write rules like this:
8
8
9
9
```
10
10
match /databases/{database}/documents/pages/{page} {
11
-
// Allow to increment only 'visits' field and only by 1.
11
+
// Allow to increment only the 'visits' field and only by 1.
12
12
match /_counter_shards_/{shardId} {
13
-
allow read;
14
-
allow create: if request.resource.data.keys().size() == 1 &&
15
-
request.resource.data.visits == 1;
16
-
allow update: if request.resource.data.keys().size() == 1 &&
Set up a [scheduled function](https://firebase.google.com/docs/functions/schedule-functions) to call `${function:controller.url}` every minute.
23
+
Review the [scheduled function documentation](https://firebase.google.com/docs/functions/schedule-functions) to set up a call to `${function:controller.url}` every minute. You may need to enable some APIs in your Firebase project to use scheduled functions.
26
24
27
-
For example, to set up a scheduled function, you can run the following [`gcloud`](https://cloud.google.com/sdk/gcloud/)command:
25
+
As an example, to set up a scheduled function, you can run the following [`gcloud`](https://cloud.google.com/sdk/gcloud/)commands:
Note: You might get a "Permission denied" error for the source repository. If you do, locate the **Sign in** button on the error page, then sign in to access to the repo.
console.log("Eventually consistent view of visits: "+snap.get("stats.views"));
70
-
})
71
-
</script>
72
-
</body>
73
-
</html>
74
-
```
38
+
1. Use the Counter SDK library in your code to increment counters. The code snippet below shows an example of how to use the library. For more comprehensive API documentation, refer to the [source code](https://dev-partners.googlesource.com/samples/firebase/mods/+/master/firestore-counter/clients/web/src/index.ts).
console.log("Eventually consistent view of visits: "+snap.get("visits"));
68
+
});
69
+
</script>
70
+
</body>
71
+
</html>
72
+
```
75
73
76
74
### Using the extension
77
75
78
-
After you complete the post-installation configuration above, your extension will create subcollections in all the documents that your app uses as counters. The extension will use these subcollections to help track the counter in a scalable way.
76
+
After you complete the post-installation configuration above, your extension will create subcollections in all the documents that your app uses as counters. The client SDK will write to these subcollections to distribute the write load, and the scheduled function you deployed will sum the subcollections' values into the single `visits` field (or whichever field you configured).
Copy file name to clipboardExpand all lines: firestore-counter/PREINSTALL.md
+14-4Lines changed: 14 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,26 @@
1
1
Use this extension to add a highly scalable counter service to your app. This is ideal for applications that count viral actions or any very high-velocity action such as views, likes, or shares.
2
2
3
-
In your app, you specify a Cloud Firestore document path and increment a field value by any amount you choose. The extension then creates a subcollection in that document to help track the counter in a scalable way.
3
+
Since Cloud Firestore has a limit of one sustained write per second, per document, this extension instead shards your writes across documents in a `_counter_shards_` subcollection. Each client only increments their own unique shard while the background workers (provided by this extension) monitor and aggregate these shards into a main document.
4
+
5
+
Here are some features of this extension:
6
+
- Scales from 0 updates per second to at least 10,000 per second.
7
+
- Supports an arbitrary number of counters in your app.
8
+
- Works offline and provides latency compensation for the main counter.
9
+
10
+
Note that this extension is currently fully resourced for use with JavaScript apps (we provide the required [JS SDK](https://dev-partners.googlesource.com/samples/firebase/mods/+/master/firestore-sharded-counter/clients/web/src/index.ts)). You can, however, use this extension on other platforms if you'd like to develop your own API based on the provided JS SDK.
4
11
5
-
Note that this extension is for use with the JavaScript apps and requires the Firebase JavaScript SDK.
6
12
7
13
#### Additional setup
8
14
9
15
Before installing this extension, make sure that you've [set up a Cloud Firestore database](https://firebase.google.com/docs/firestore/quickstart) in your Firebase project.
10
16
11
-
After installation, you'll need to update your database security rules and set up a [scheduled function](https://firebase.google.com/docs/functions/schedule-functions) to regularly call one of the functions created by this extension. Detailed information for these post-installation tasks are provided after you install this extension.
17
+
After installing this extension, you'll need to:
18
+
- Update your [database security rules](https://firebase.google.com/docs/rules).
19
+
- Set up a [scheduled function](https://firebase.google.com/docs/functions/schedule-functions) to regularly call the controller function, which is created by this extension and monitors the extension's workload.
20
+
- Install the provided [Counter SDK](https://dev-partners.googlesource.com/samples/firebase/mods/+/master/firestore-sharded-counter/clients/web/src/index.ts) in your app. You can then use this library in your code to specify your document path and increment values.
21
+
22
+
Detailed information for these post-installation tasks are provided after you install this extension.
12
23
13
-
This extension provides a Counter SDK that you need to install in your app. You can then use this library in your code to specify your document path and increment values. Detailed instructions to install this SDK and use it are provided after you install this extension.
0 commit comments