# POST /v1/poll

URL: https://docs.folkways.io/docs/realtime-api/poll

Receive events over HTTP long-polling, with stateless cursors and state versions, where WebSockets are blocked.

```http
POST https://realtime.folkways.net/v1/poll
Authorization: Bearer <token>
Content-Type: application/octet-stream

<one client packet with one Poll frame>
```

A stateless read that can wait for new events. The server keeps nothing between polls: each request sends the
positions to read from, and the response carries the events after them.

<Diagram name="long-poll" alt="The client sends a Poll for rooms/1 after cursor 7:41 with a 25-second wait. The server waits for something new, then answers 200 with a SubAck, a Deliver with event 7:42 and a PollEnd; the client saves cursor 7:42 and polls again at once, keeping one poll at a time. When nothing arrives within 25 seconds it gets SubAck and PollEnd with timed_out, and polls again. A 401 Close with AuthExpired means fetch a new token and resend the same request. A 503 Close with Draining and a retry delay means wait, then retry." />

[Build a client in another language](/docs/realtime-api/build-a-client) has a complete, tested Python client that
encodes polls, decodes replies and runs this loop.

## The Poll frame [#the-poll-frame]

| Field | Default | Meaning |
| --- | --- | --- |
| `topics` | required | 1 to 256 [`Subscribe` entries](/docs/realtime-api/subscribe#the-subscribe-frame), with `start`, `cursor`, `on_gap` and `types` as on a WebSocket |
| `max_events` | 100 | Events across all topics, at most 1,000 |
| `wait_ms` | | How long to wait when nothing is available yet, at most 25,000. `0` returns at once |

## Response [#response]

`200` with `application/octet-stream`: server packets, each prefixed by its little-endian `u32` length. All frames
carry the poll's `corr`:

1. for each topic: an optional `Gap`, then `SubAck`, then `Deliver` frames, or `Snapshot` frames for KV and signals;
2. finally `PollEnd { timed_out, more }`.

`more: true` means more events are waiting: poll again at once.

## The next poll [#the-next-poll]

- For stream events, set each topic's `start: After` and `cursor` to the last event you received.
- For KV and signals in a **one-type** topic, a poll with a cursor from the same epoch returns only keys changed
  after it, including deletions; without one, a snapshot.
- For KV and signals in a topic with **several types**, send `state_after` set to the last `SubAck.state_latest`.
  You get only the changes after it; without it, a snapshot. `state_latest` comes in every answer, even an empty one,
  so the next poll can wait.

Signals between polls are best effort.

## Draining [#draining]

When the server starts draining, a waiting poll returns at once with `PollEnd { timed_out: true }`. The next request
to that server gets `503` with a `Close { Draining, retry_after_ms }` frame; retry after the delay.

## Errors [#errors]

Per-topic failures are `OpError` frames inside a `200`. An authentication or packet-level failure returns the code's
HTTP status with a `Close` frame. CORS allows any origin, as for [`/v1/publish`](/docs/realtime-api/publish#post-v1publish).

## Running a poll loop [#running-a-poll-loop]

- **One poll at a time.** Send the next poll when the previous one ends; overlapping polls receive the same events
  twice.
- **Match replies by `topic_id`,** learned from each topic's `SubAck`. A topic the server refuses outright is
  reported with `topic_id` 0, ahead of the other topics' replies, so learn each new topic's id with a one-topic poll
  (`wait_ms: 0`) first.
- **Refresh the token between polls,** a minute before it expires. A poll refused with `AuthExpired` can be sent again
  with a new token.
- **Retry** `RateLimited`, `Unavailable`, `Draining` and `Timeout` after `retry_after_ms`, with backoff and jitter. Don't
  retry `AuthInvalid`, `Forbidden`, `Suspended` or `AuthRevoked`.
