# Live listeners
Source: https://sovaria.in/docs/live-listeners

Get a document or a query's results, then every change to them as it happens: added, modified and removed.

A live listener is a query (or one document) that stays open. You get the current results first, then a snapshot with the changes every time a write affects them. Each listener sees only what its own user may read: the query re-runs as that user, with your collection rules and row policies.

(Diagram: How a change reaches a listener)

JavaScript:
```js
const stop = await sov.collection('rooms/r1/messages')
  .orderBy('at', 'desc').limit(50)
  .onSnapshot((snap) => {
    render(snap.docs)
    for (const c of snap.changes) console.log(c.type, c.doc.id)   // added | modified | removed
  }, (err) => console.warn(err))

const stopDoc = await sov.doc('users/alice').onSnapshot((doc) => setProfile(doc.data))
stop(); stopDoc()
```

Python:
```python
# pip install "sovaria-sdk[realtime]" for WebSocket delivery (otherwise it polls)
async def on_change(snap):
    for change in snap.changes:
        print(change.type, change.doc.id)

listener = await sov.collection("orders").where("status", "==", "open").on_snapshot(on_change)
...
await listener.close()
```

HTTP:
```bash
curl -s -X POST $URL/api/v1/data/doc/_listen/orders -d '{"where":[["status","==","open"]]}' …
# → {"id","channel":"doclisten:<id>","documents":[…],"ttl_seconds":600}
# Subscribe to the channel over realtime; renew before it expires:
curl -s -X POST $URL/api/v1/data/doc/_listeners/<id>/renew …
# Many listeners: add "client_channel":"<16-64 random hex>" to each _listen so they share one channel
# (updates carry the listener id), and renew them together (at most 200 ids):
curl -s -X POST $URL/api/v1/data/doc/_listeners/renew -d '{"ids":["<id>","<id2>"]}' …
# → {"listeners":{"<id>":{…},"<id2>":{"error":"NOT_FOUND","status":404}}}
curl -s -X DELETE $URL/api/v1/data/doc/_listeners/<id> …
```

## How it behaves

- Only the user who started a listener can subscribe to its channel, and nobody can publish on it.
- All of a client's listeners share one realtime connection: the JavaScript SDK sends a random per-client `client_channel`, so ten `onSnapshot`s are one WebSocket and one reconnect loop, and they are renewed in one request. The shared channel belongs to that client and its signed-in user only.
- A listener doesn't run its query twice on start: once the connection is up, the SDK re-reads only if a change landed in between.
- Listeners expire after 10 minutes unless renewed; the SDKs renew every 5 minutes. Renewing checks the collection rules again, so revoked access stops within one renewal.
- After a reconnect, or when an update was missed, the SDKs re-run the query once and report the difference, so a snapshot never silently drifts.
- Writes to collections nobody listens to cost nothing extra.
- Collection groups: `sov.collectionGroup('comments').onSnapshot(…)` (or `"collection_group": true` on `_listen`) listens to every collection with that id; documents are keyed by their full path, and each renewal re-checks the rules per collection pattern.
- Each update is one realtime message and counts toward monthly realtime messages; listener channels don't count toward realtime channels.

## Plan limits

| Limit | Free | Pro | Ultra | Team | Enterprise |
|---|---|---|---|---|---|
| Live listeners open at once (`docs.max_listeners`) | 100 | 2,000 | 10,000 | 25,000 | 100,000 |
| Documents one listener tracks (`docs.listener_max_docs`) | 500 | 1,000 | 2,000 | 5,000 | 10,000 |

Over the cap, starting a listener returns `403 PLAN_LIMIT_REACHED`.
