> 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-almtw-ryn/websocket-subscriptions.md).

# اشتراكات WebSocket

اشترك في أحداث Rome EVM عبر WebSocket — استخدم eth\_subscribe مع newHeads وlogs، ويعمل مباشرةً مع ethers؛ ومع viem، استخدم الاستطلاع الدوري.

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

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

هذا ما يستخدمه `ethers` `WebSocketProvider` في الخلفية، لذا `.on('block')` و `contract.on(event)` يعمل مباشرة دون أي إعداد. `viem` يحتاج إلى إعداد واحد، موضح في قسم [viem](#viem) .

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

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

## ethers (v6)

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

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

// newHeads
provider.on("block", (blockNumber) => {
  console.log("كتلة جديدة", blockNumber);
});

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

## viem

`viem`الخاص بـ `webSocket` يسجل النقل اشتراكًا فقط عندما يكون المعرّف سلسلة نصية. تُرجع Rome رقمًا، لذا `watchBlocks` فوق نقلٍ عادي `webSocket` لا يستقبل النقل أي شيء. مرّر `poll: true` و `viem` يستقصي عبر الاتصال بدلًا من ذلك (تم التحقق باستخدام `watchBlocks`), أو استخدم `ethers` للتحديثات الفورية.

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

client.watchEvent({
  poll: true,
  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":8704545703806774}            // معرّف الاشتراك
← {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":8704545703806774,"result":{…block…}}}
```

معرّف الاشتراك هو رقم JSON، وليس سلسلة سداسية عشرية، وتحمل الإشعارات الرقم نفسه. `ethers` يتولى هذا الأمر نيابةً عنك. `viem`الخاص بـ `webSocket` يقبل النقل فقط معرّفًا كسلسلة نصية، لذا يتجاهل الإشعارات (انظر [viem](#viem)). إذا كتبتَ عميلًا خامًا، فقارن المعرفات كأرقام وأعد تمرير الرقم إلى `eth_unsubscribe`.

لـ `logs`، مرّر كائن فلتر باعتباره المعامل الثاني:

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

## ملاحظات

* تُرسل الاشتراكات التحديثات فور إنتاج كتل جديدة (زمن التأخير يعادل تقريبًا زمن الكتلة).
* للاتصالات طويلة الأمد، تعامل مع إعادة الاتصال في عميلك — وهو أمر قياسي لأي RPC عبر WebSocket.


---

# 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/ar/adlh-almtw-ryn/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.
