# Realtime Updates

PaperMarket consumes Polymarket data in two modes\
on demand snapshots for simulation and a continuous stream for live display.

Snapshots are used for execution and valuation logic.\
The real time stream is used to keep what the user sees in sync with the current state of the Polymarket order book and prices.

### Websocket subscriptions

For markets that are currently active in the interface PaperMarket opens a websocket connection to Polymarket subscription endpoints.

Through this connection the app subscribes to channels for the relevant YES and NO tokens.\
Incoming messages typically include

* incremental changes to bids and asks
* updates to best bid and best ask levels
* occasionally last traded prices or other event data depending on the Polymarket stream format

These messages are applied to the in memory representation of the book and price state used by the frontend so that the displayed depth and current prices follow Polymarket in near real time.

### Separation of stream and execution

The real time stream is not used directly to simulate trades.

Even if the frontend has just applied several websocket updates the backend does not rely on that state for execution.\
Before a simulated order is matched the backend always performs an explicit snapshot request to the Polymarket CLOB API.

This separation has two important effects

* live updates keep the UI responsive and informative
* execution is always based on a single coherent snapshot instead of a sequence of partial stream messages

If the websocket stream lags or drops temporarily the simulation logic still depends on fresh snapshot data rather than whatever the UI last rendered.

### Live chart and mid price updates

Real time messages are also used to refresh derived values without extra polling.

When an update for a token arrives the app can

* adjust the best bid and best ask levels in the local book
* recompute the mid price used for on screen valuation
* append or adjust the latest point on the short term chart segment if the format allows

This keeps the visible mid price and recent price path closely aligned with Polymarket between full history refreshes.

The underlying historical time series still comes from explicit calls to Polymarket history endpoints\
real time updates only affect the most recent part of what is drawn.

### Handling disconnects and reconnection

Websocket connections to external services are not guaranteed to be permanent.\
PaperMarket treats the Polymarket stream as an optional enhancement rather than a hard dependency.

Typical behaviour includes

* detecting when the stream connection is closed or idle for too long
* attempting to reconnect with backoff
* resetting local in memory book state by fetching a fresh snapshot after a successful reconnect

If the stream is unavailable for a period of time

* existing positions and PnL can still be valued using periodic snapshot requests
* trade simulation continues to rely on direct CLOB calls
* only the live animation of depth and prices in the interface becomes less frequent or temporarily static

### Consistency guarantees

The combination of stream and snapshot access gives PaperMarket a clear consistency model

* UI can move quickly based on best effort live updates
* critical operations such as trade simulation and position valuation rely on explicit snapshots
* no fills are ever generated purely from speculative or inferred data

Real time updates make the simulation feel close to the live Polymarket market\
snapshots ensure that when PaperMarket decides at what prices trades would have executed it always does so from a well defined view of the order book.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://papermarket.gitbook.io/papermarket/market-data-and-charts/realtime-updates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
