> 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/id/panduan-pengembang/websocket-subscriptions.md).

# Langganan WebSocket

Berlangganan event Rome EVM melalui WebSocket — eth\_subscribe dengan newHeads dan logs, siap pakai dengan ethers; dengan viem, gunakan polling.

Endpoint Rome EVM mendukung langganan WebSocket Ethereum (`eth_subscribe` / `eth_unsubscribe`) di atas `wss://`. Sambungkan ke host yang sama dengan RPC HTTPS, dengan `wss://` skema:

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

Inilah yang `ethers` `WebSocketProvider` digunakan di balik layar, jadi `.on('block')` dan `contract.on(event)` langsung berfungsi. `viem` memerlukan satu pengaturan, yang dijelaskan di [viem](#viem) bagian ini.

## Jenis langganan

| Jenis                    | Dipicu saat                                            | Digunakan untuk                                                               |
| ------------------------ | ------------------------------------------------------ | ----------------------------------------------------------------------------- |
| `newHeads`               | setiap blok baru                                       | ticker blok, menyegarkan saldo/nonce, bot per-blok, pengindeks                |
| `logs`                   | peristiwa yang cocok (berdasarkan `address` / `topik`) | bereaksi terhadap peristiwa kontrak Anda, relayer/bridge, bot trading         |
| `newPendingTransactions` | —                                                      | diterima tetapi tidak pernah dipancarkan (Rome tidak memiliki mempool publik) |

## ethers (v6)

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

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

// newHeads
provider.on("block", (blockNumber) => {
  console.log("blok baru", blockNumber);
});

// logs — bereaksi terhadap event kontrak
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` transport mendaftarkan langganan hanya ketika id berupa string. Rome mengembalikan angka, jadi `watchBlocks` pada transport biasa `webSocket` transport tidak menerima apa pun. Berikan `poll: true` dan `viem` melakukan polling melalui koneksi sebagai gantinya (diperiksa dengan `watchBlocks`) `ethers` untuk pembaruan push.

```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("blok baru", block.number),
});

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

## JSON-RPC mentah

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

Id langganan adalah angka JSON, bukan string hex, dan notifikasi membawa angka yang sama. `ethers` menangani ini untuk Anda. `viem`'s `webSocket` transport hanya menerima id string, jadi ia membuang notifikasi (lihat [viem](#viem)). Jika Anda menulis klien mentah, bandingkan id sebagai angka dan kirimkan angka itu kembali ke `eth_unsubscribe`.

Untuk `logs`, berikan objek filter sebagai parameter kedua:

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

## Catatan

* Langganan mengirim pembaruan saat blok baru diproduksi (latensinya seorde waktu satu blok).
* Untuk koneksi jangka panjang, tangani penyambungan ulang di klien Anda — standar untuk RPC WebSocket apa pun.


---

# 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/id/panduan-pengembang/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.
