> 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 EVM Rome mendukung langganan WebSocket Ethereum (`eth_subscribe` / `eth_unsubscribe`) di atas `wss://`). Sambungkan ke host yang sama seperti RPC HTTPS, dengan `wss://` skema:

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

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

## Jenis langganan

| Jenis                    | Dipicu pada                                       | Digunakan untuk                                                                        |
| ------------------------ | ------------------------------------------------- | -------------------------------------------------------------------------------------- |
| `newHeads`               | setiap blok baru                                  | ticker blok, menyegarkan saldo/nonce, bot per blok, pengindeks                         |
| `logs`                   | event yang cocok (berdasarkan `alamat` / `topik`) | bereaksi terhadap event kontrak Anda, relayer/jembatan, bot perdagangan                |
| `newPendingTransactions` | —                                                 | diterima tetapi tidak pernah menghasilkan apa pun (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 hanya mendaftarkan langganan ketika id berupa string. Rome mengembalikan angka, jadi `watchBlocks` di atas transport biasa `webSocket` tidak menerima apa pun. Berikan `poll: true` dan `viem` poll di atas koneksi sebagai gantinya (dicek dengan `watchBlocks`), atau gunakan `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":{…blok…}}}
```

ID langganan adalah angka JSON, bukan string hex, dan notifikasinya membawa angka yang sama. `ethers` menanganinya untuk Anda. `viem`'s `webSocket` transport hanya menerima id string, jadi notifikasinya dibuang (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 dihasilkan (latensinya kira-kira setara dengan waktu 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.
