> 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/ar/adlh-almtwryn/websocket-subscriptions.md).

# اشتراكات WebSocket

تدعم نقاط نهاية Rome EVM اشتراكات Ethereum عبر WebSocket (`eth_subscribe` / `eth_unsubscribe`) عبر `wss://`. اتصل بنفس المضيف الخاص بـ HTTPS RPC، مع `wss://` المخطط:

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

هذا ما `ethers` / `viem` `WebSocketProvider` تستخدمه في الخلفية، لذا `.on('block')`, `contract.on(event)`، و `viem`لـ `watchEvent` / `watchBlocks` تعمل مباشرةً دون إعدادات إضافية.

## أنواع الاشتراكات

| النوع                        | يحدث عند                                       | يُستخدم لـ                                                          |
| ---------------------------- | ---------------------------------------------- | ------------------------------------------------------------------- |
| `رؤوس الكتل الجديدة`         | كل كتلة جديدة                                  | متتبعات الكتل، تحديث الأرصدة/الـnonces، روبوتات لكل كتلة، المفهرسات |
| `السجلات`                    | الأحداث المطابقة (حسب `العنوان` / `الموضوعات`) | الاستجابة لأحداث عقدك، والمرحلات/الجسور، وروبوتات التداول           |
| `المعاملات المعلّقة الجديدة` | —                                              | يُقبل لكنه لا يصدر أبدًا (لا تحتوي Rome على mempool عام)            |

## ethers (الإصدار 6)

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

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

// رؤوس الكتل الجديدة
provider.on("block", (blockNumber) => {
  console.log("new block", blockNumber);
});

// السجلات — التفاعل مع حدث عقد
const contract = new ethers.Contract(address, abi, provider);
contract.on("Transfer", (from, to, value) => {
  console.log("Transfer", from, to, value.toString());
});
```

## viem

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

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

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

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

## JSON-RPC الخام

```json
→ {"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["newHeads"]}
← {"jsonrpc":"2.0","id":1,"result":"0x…"}                       // معرف الاشتراك
← {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x…","result":{…block…}}}
```

لـ `السجلات`، مرّر كائن تصفية كالمعامل الثاني:

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

## ملاحظات

* تدفع الاشتراكات التحديثات مع إنتاج كتل جديدة (زمن التأخير في حدود زمن الكتلة).
* بالنسبة للاتصالات طويلة الأمد، تعامل مع عمليات إعادة الاتصال في عميلك — وهذا أمر قياسي في أي 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 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://docs.rome.builders/ar/adlh-almtwryn/websocket-subscriptions.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.
