2023-04-17 12:31:18 -04:00
# NIP-45
2023-01-03 23:11:17 -05:00
2023-04-17 12:31:18 -04:00
## Event Counts
2023-01-03 23:11:17 -05:00
`draft` `optional` `author:staab`
2023-04-07 08:08:03 -04:00
Relays may support the `COUNT` verb, which provides a mechanism for obtaining event counts.
2023-01-03 23:11:17 -05:00
## Motivation
Some queries a client may want to execute against connected relays are prohibitively expensive, for example, in order to retrieve follower counts for a given pubkey, a client must query all kind-3 events referring to a given pubkey and count them. The result may be cached, either by a client or by a separate indexing server as an alternative, but both options erode the decentralization of the network by creating a second-layer protocol on top of Nostr.
## Filters and return values
2023-04-12 11:18:22 -04:00
This NIP defines a verb called `COUNT` , which accepts a subscription id and filters as specified in [NIP 01 ](01.md ).
2023-04-17 12:31:18 -04:00
```json
2023-04-12 11:18:22 -04:00
["COUNT", < subscription_id > , < filters JSON > ...]
```
2023-01-03 23:11:17 -05:00
Counts are returned using a `COUNT` response in the form `{count: <integer>}` . Relays may use probabilistic counts to reduce compute requirements.
2023-04-17 12:31:18 -04:00
```json
2023-04-12 11:18:22 -04:00
["COUNT", < subscription_id > , {"count": < integer > }]
```
2023-01-03 23:11:17 -05:00
Examples:
2023-04-17 12:31:18 -04:00
```json
2023-01-03 23:11:17 -05:00
# Followers count
2023-04-12 11:18:22 -04:00
["COUNT", < subscription_id > , {"kinds": [3], "#p": [< pubkey > ]}]
["COUNT", < subscription_id > , {"count": 238}]
2023-01-03 23:11:17 -05:00
# Count posts and reactions
2023-04-12 11:18:22 -04:00
["COUNT", < subscription_id > , {"kinds": [1, 7], "authors": [< pubkey > ]}]
["COUNT", < subscription_id > , {"count": 5}]
2023-01-03 23:11:17 -05:00
```