Skip to content

Commit 5c92a5b

Browse files
author
Patryk Lesiewicz
committed
Remove real changes.
1 parent 709be87 commit 5c92a5b

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed

firestore-counter/POSTINSTALL.md

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@ Before you can use this extension, you'll need to update your security rules, se
44

55
#### Update security rules
66

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:
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:
88

99
```
1010
match /databases/{database}/documents/pages/{page} {
11-
// Allow to increment only the 'visits' field and only by 1.
11+
// Allow to increment only 'visits' field and only by 1.
1212
match /_counter_shards_/{shardId} {
13-
allow get;
14-
allow write: if request.resource.data.keys() == ["visits"]
15-
&& (resource == null || request.resource.data.visits ==
16-
resource.data.visits + 1);
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 &&
17+
request.resource.data.visits == resource.data.visits + 1;
18+
allow delete: if false;
1719
}
1820
}
1921
```
2022

2123
#### Set up a scheduled function
2224

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.
25+
Set up a [scheduled function](https://firebase.google.com/docs/functions/schedule-functions) to call `${function:controller.url}` every minute.
2426

25-
As an example, to set up a scheduled function, you can run the following [`gcloud`](https://cloud.google.com/sdk/gcloud/) commands:
27+
For example, to set up a scheduled function, you can run the following [`gcloud`](https://cloud.google.com/sdk/gcloud/) command:
2628

2729
```
28-
gcloud services enable cloudscheduler.googleapis.com
2930
gcloud scheduler jobs create http firestore-sharded-counter-controller --schedule="* * * * *" --uri=${function:controller.url} --project=${param:PROJECT_ID}
3031
```
3132

@@ -35,41 +36,42 @@ gcloud scheduler jobs create http firestore-sharded-counter-controller --schedul
3536

3637
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.
3738

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).
39-
40-
```html
41-
<html>
42-
<head>
43-
<script src="https://www.gstatic.com/firebasejs/[version]/firebase-app.js"></script>
44-
<script src="https://www.gstatic.com/firebasejs/[version]/firebase-firestore.js"></script>
45-
<script src="sharded-counter.js"></script>
46-
</head>
47-
<body>
48-
<script>
49-
// Initialize Firebase.
50-
var firebaseConfig = { projectId: "${PROJECT_ID}" };
51-
firebase.initializeApp(firebaseConfig);
52-
var db = firebase.firestore();
53-
54-
// Initialize the sharded counter.
55-
var visits = new sharded.Counter(db.doc("pages/hello-world"), "stats.visits");
56-
57-
// Increment the field "stats.visits" of the document "pages/hello-world".
58-
visits.incrementBy(1);
59-
60-
// Listen to locally consistent values.
61-
visits.onSnapshot((snap) => {
62-
console.log("Locally consistent view of visits: " + snap.data());
63-
});
64-
65-
// Alternatively, if you don't mind counter delays, you can listen to the document directly.
66-
db.doc("pages/hello-world").onSnapshot((snap) => {
67-
console.log("Eventually consistent view of visits: " + snap.get("stats.visits"));
68-
});
69-
</script>
70-
</body>
71-
</html>
72-
```
39+
1. Use the Counter SDK library in your code:
40+
41+
```html
42+
<html>
43+
<head>
44+
<script src="https://www.gstatic.com/firebasejs/[version]/firebase-app.js"></script>
45+
<script src="https://www.gstatic.com/firebasejs/[version]/firebase-firestore.js"></script>
46+
<script src="sharded-counter.js"></script>
47+
</head>
48+
<body>
49+
<script>
50+
// Initialize Firebase.
51+
var firebaseConfig = { projectId: "${PROJECT_ID}" };
52+
firebase.initializeApp(firebaseConfig);
53+
var db = firebase.firestore();
54+
55+
// Initialize the sharded counter.
56+
var views = new sharded.Counter(db.doc("pages/hello-world"), "stats.views");
57+
58+
// Increment by 3 the field "stats.views" of the document: ${param:MOD_METADATA_DOC}.
59+
// (use your desired increment amount)
60+
views.incrementBy(3);
61+
62+
// Listen to locally consistent values.
63+
views.onSnapshot((snap) => {
64+
console.log("Locally consistent view of visits: " + snap.data());
65+
});
66+
67+
// Alternatively, if you don't mind counter delays, you can listen to the document directly.
68+
db.doc("pages/hello-world").onSnapshot((snap) => {
69+
console.log("Eventually consistent view of visits: " + snap.get("stats.views"));
70+
})
71+
</script>
72+
</body>
73+
</html>
74+
```
7375

7476
### Using the extension
7577

firestore-counter/PREINSTALL.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
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.
22

3-
Cloud Firestore has a limit of 1 sustained write per second, per document. This limit can make counting in Cloud Firestore challenging.
4-
This extension works around the concurrent document writes per second limitation and lets your app update any field in any document of your Cloud Firestore database at a high rate (security rules permitting). It accomplishes this by using numerous temporary shards in a `_counter_shards_` subcollection; allowing it to sustain high bursts of traffic. Each client only increments their own unique shard while the background workers (Cloud Functions for Firebase) continue to monitor and aggregate these shards onto the master document.
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.
54

6-
Here are some of the important features of this extension:
7-
8-
- Minimal configuration, one extension for all your app needs.
9-
- Automatically scales from 0 updates per second to at least 10 thousand per second.
10-
- Supports an arbitrary number of counters in your app with no additional configuration.
11-
- Works well offline and provides latency compensation, i.e. counter updates are immediately visible locally even though the main counter is eventually updated.
12-
- Resource efficient.
13-
- `worker` functions will scale down to 0 for low workloads if needed.
14-
- `onWrite` function is [limted to 1 instance](https://cloud.google.com/functions/docs/max-instances#using_max_instances).
15-
- `controller` is the only function that runs every minute regardless of the workload.
16-
17-
This mod can work on any platform. However there's only [JavaScript SDK](https://dev-partners.googlesource.com/samples/firebase/mods/+/master/firestore-sharded-counter/clients/web/src/index.ts) included at this time. This will change in the future.
5+
Note that this extension is for use with the JavaScript apps and requires the Firebase JavaScript SDK.
186

197
#### Additional setup
208

0 commit comments

Comments
 (0)