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

# WebSocket-Abonnements

Abonniere Rome-EVM-Ereignisse über WebSocket — eth\_subscribe mit newHeads und logs, sofort einsatzbereit mit ethers; mit viem verwende Polling.

Rome EVM-Endpunkte unterstützen Ethereum-WebSocket-Abonnements (`eth_subscribe` / `eth_unsubscribe`) über `wss://`. Verbinde dich mit demselben Host wie das HTTPS-RPC, mit dem `wss://` Schema:

```
wss://<chain>.<network>.romeprotocol.xyz/
```

Das ist, was `ethers` `WebSocketProvider` unter der Haube verwendet, also `.on('block')` und `contract.on(event)` funktioniert direkt ohne weiteres. `viem` benötigt eine Einstellung, beschrieben im [viem](#viem) Abschnitt.

## Abonnementtypen

| Typ                      | Wird ausgelöst bei                                   | Verwenden für                                                                    |
| ------------------------ | ---------------------------------------------------- | -------------------------------------------------------------------------------- |
| `newHeads`               | jedem neuen Block                                    | Block-Ticker, Aktualisieren von Guthaben/Nonces, Bots pro Block, Indexer         |
| `logs`                   | übereinstimmenden Events (nach `Adresse` / `Topics`) | Reagieren auf die Events deines Contracts, Relayer/Bridges, Trading-Bots         |
| `newPendingTransactions` | —                                                    | akzeptiert, sendet aber niemals etwas aus (Rome hat keinen öffentlichen Mempool) |

## ethers (v6)

```javascript
import { ethers } from "ethers";

const provider = new ethers.WebSocketProvider(
  "wss://hadrian.testnet.romeprotocol.xyz/"
);

// neue Blockköpfe
provider.on("block", (blockNumber) => {
  console.log("neuer Block", blockNumber);
});

// logs — auf ein Contract-Event reagieren
const contract = new ethers.Contract(address, abi, provider);
contract.on("Transfer", (from, to, value) => {
  console.log("Transfer", from, to, value.toString());
});
```

## viem

`viem`'s `webSocket` Der Transport registriert ein Abonnement nur, wenn die ID ein String ist. Rome gibt eine Zahl zurück, also `watchBlocks` über einen normalen `webSocket` erhält der Transport nichts. Übergib `poll: true` und `viem` pollt stattdessen über die Verbindung hinweg (überprüft mit `watchBlocks`), oder verwende `ethers` für Push-Updates.

```javascript
import { createPublicClient, webSocket } from "viem";

const client = createPublicClient({
  transport: webSocket("wss://hadrian.testnet.romeprotocol.xyz/"),
});

const unwatch = client.watchBlocks({
  poll: true,
  onBlock: (block) => console.log("neuer Block", block.number),
});

client.watchEvent({
  poll: true,
  address,
  onLogs: (logs) => console.log(logs),
});
```

## Rohes JSON-RPC

```json
→ {"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["newHeads"]}
← {"jsonrpc":"2.0","id":1,"result":8704545703806774}            // Abonnement-ID
← {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":8704545703806774,"result":{…block…}}}
```

Die Abonnement-ID ist eine JSON-Zahl, keine Hex-Zeichenkette, und die Benachrichtigungen tragen dieselbe Nummer. `ethers` handhabt das für dich. `viem`'s `webSocket` der Transport akzeptiert nur eine String-ID, daher verwirft er die Benachrichtigungen (siehe [viem](#viem)). Wenn du einen Rohclient schreibst, vergleiche IDs als Zahlen und gib die Zahl zurück an `eth_unsubscribe`.

Für `logs`, übergib als zweites Argument ein Filterobjekt:

```json
{"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["logs",{"address":"0x…","topics":["0x…"]}]}
```

## Hinweise

* Abonnements senden Updates, sobald neue Blöcke erzeugt werden (die Latenz liegt in der Größenordnung einer Blockzeit).
* Bei langfristigen Verbindungen solltest du in deinem Client Wiederverbindungen behandeln — üblich für jede WebSocket-RPC.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rome.builders/de/entwicklerleitfaden/websocket-subscriptions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
