> For the complete documentation index, see [llms.txt](https://svpchain.gitbook.io/svpchain-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://svpchain.gitbook.io/svpchain-docs/build/websocket.md).

# WebSocket

WebSocket subscriptions — newHeads, logs, and reconnect strategy.

The WebSocket endpoint serves the standard `eth_subscribe` interface — same as on Ethereum. Use it for realtime feeds of new blocks and logs. For one-shot reads, prefer [JSON-RPC](/svpchain-docs/build/json-rpc.md) over HTTP.

* **Testnet:** `wss://svp-dataseeds-testnet.svpchain.org/ws`
* **Mainnet:** `wss://svp-dataseeds.svpchain.org/ws`
* Alternates: [Networks](/svpchain-docs/chain/networks.md).

### Subscriptions

| Subscription             | What it streams                                                      |
| ------------------------ | -------------------------------------------------------------------- |
| `newHeads`               | Block headers as they are committed (\~1/sec).                       |
| `logs`                   | Event logs matching the given filter (`address`, `topics`).          |
| `newPendingTransactions` | Tx hashes entering the mempool. **Best-effort** on public endpoints. |

### Examples

#### Subscribe to new heads

```sh
echo '{"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["newHeads"]}' \
  | websocat wss://svp-dataseeds-testnet.svpchain.org/ws
```

The first response gives you a subscription ID; every subsequent message is a new block header.

#### Subscribe to logs from a contract

```sh
echo '{
  "jsonrpc":"2.0","id":1,"method":"eth_subscribe",
  "params":["logs",{
    "address":"0xYourContract",
    "topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]
  }]
}' | websocat wss://svp-dataseeds-testnet.svpchain.org/ws
```

The `topics[0]` above is `keccak256("Transfer(address,address,uint256)")`.

#### Viem (TypeScript)

```ts
import { createPublicClient, webSocket, parseAbiItem } from "viem";

const client = createPublicClient({
  transport: webSocket("wss://svp-dataseeds-testnet.svpchain.org/ws"),
});

client.watchBlockNumber({ onBlockNumber: (n) => console.log("head", n) });

client.watchEvent({
  address: "0xYourContract",
  event: parseAbiItem(
    "event Transfer(address indexed from, address indexed to, uint256 value)"
  ),
  onLogs: (logs) => console.log(logs),
});
```

### Recommended pattern

1. **Persist a cursor.** Save the last fully-processed block number to durable storage.
2. **Subscribe.** Open `newHeads` (or `logs`) and process events as they arrive.
3. **Confirm via JSON-RPC.** For each event, use HTTP RPC (`eth_getBlockByNumber`, `eth_getLogs`) as the canonical source — WS is a notification stream, not a database.
4. **Backfill on reconnect.** When the socket drops, read the cursor, call `eth_getLogs(cursor+1, currentHead)`, then resume.

### Reconnect strategy

* Use exponential backoff with a cap (e.g. 250ms → 8s).
* On any error or close event, reset and reconnect.
* Don't trust the socket-level "still alive" signal — also alert if the cursor stops advancing for more than a few seconds while the chain is producing blocks.

### SVPChain-specific notes

* **No reorgs.** Every committed block is final. You don't need a confirmation buffer or "unwind on reorg" code path.
* **CLOB events** are not in `eth_subscribe("logs", ...)`. To stream order-book activity, subscribe to **CometBFT** block events on the Cosmos surface. Endpoint and event schema: TODO.
* **`newPendingTransactions`** is best-effort on public endpoints — run your own node for a reliable mempool feed.

### Next steps

<table data-view="cards"><thead><tr><th>Related</th><th data-card-target data-type="content-ref">Link</th></tr></thead><tbody><tr><td>JSON-RPC reference</td><td><a href="/svpchain-docs/build/json-rpc.md">JSON-RPC</a></td></tr><tr><td>Indexing patterns</td><td><a href="/svpchain-docs/build/indexing-and-events.md">Indexing &amp; events</a></td></tr><tr><td>Run your own node</td><td><a href="/svpchain-docs/chain/run-a-node.md">Run a node</a></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://svpchain.gitbook.io/svpchain-docs/build/websocket.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
