X Layer provides robust WebSocket (WSS) support for Ethereum-compatible methods and ZKEVM RPC methods, enabling real-time, bidirectional communication between clients and servers. This guide explores the technical specifications, endpoints, and subscription methods available on both Mainnet and Testnet.
Understanding WebSockets in X Layer
WebSockets revolutionize communication by maintaining a persistent TCP connection, allowing for low-latency, real-time data exchange. Unlike HTTP’s request-response model, WebSockets facilitate ongoing two-way interactions—ideal for decentralized applications (DApps) requiring instant updates.
Key Benefits:
- Real-time event tracking: Subscribe to blockchain events like new blocks or logs without polling.
- Reduced latency: Eliminates repeated connection overhead.
- Scalability: Efficiently handles high-frequency data streams.
X Layer WebSocket Endpoints
Mainnet (Chain ID: 0xC4 or 196)
👉 Connect to Mainnet WebSocket
Endpoints:
wss://xlayerws.okx.comwss://ws.xlayer.tech
Testnet (Chain ID: 0xC3 or 195)
Endpoints:
wss://xlayertestws.okx.comwss://testws.xlayer.tech
WebSocket Subscription Methods
eth_subscribe
Create subscriptions for specific Ethereum events:
newHeads- Tracks new block headers, including chain reorganizations.
Example:
{"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["newHeads"]}
logs- Filters logs by address/topics. Reorganizations trigger re-emissions with a
removedflag. Example:
{"jsonrpc":"2.0","id":2,"method":"eth_subscribe","params":["logs",{"address":"0x00...","topics":["0x00..."]}]}
- Filters logs by address/topics. Reorganizations trigger re-emissions with a
Unsupported Events
newPendingTransactions: Not available on X Layer.
Returns: A unique subscription ID.
eth_unsubscribe
Cancel active subscriptions:
- Parameter:
subscription ID(frometh_subscribe). - Returns:
trueif successful. Example:
{"jsonrpc":"2.0","id":3,"method":"eth_unsubscribe","params":["0xabcd1234"]}
Supported RPC Methods
X Layer’s WebSocket interface supports:
- Ethereum-compatible RPCs (e.g.,
eth_blockNumber). - ZKEVM-specific methods (detailed in the RPC documentation).
Prerequisite: Install a WebSocket client like ws.
FAQ
1. How do WebSockets improve DApp performance?
WebSockets eliminate polling, reducing latency and server load by pushing updates instantly.
2. Can I use HTTP and WebSocket endpoints interchangeably?
No. WebSockets are for real-time subscriptions; HTTP is for one-off requests.
3. What happens during a chain reorganization?
Subscriptions to newHeads or logs will re-emit data for the new chain branch, marked appropriately.
4. Are there rate limits for WebSocket connections?
Check X Layer’s documentation for current rate-limiting policies.
5. How do I handle connection drops?
Implement reconnection logic and session resumption where supported.
6. Is SSL/TLS encryption used for WSS?
Yes, all endpoints use wss:// for secure communication.
Conclusion
👉 Start integrating X Layer WebSockets today for seamless, real-time blockchain interactions. Whether building DeFi platforms or monitoring tools, leveraging WebSocket endpoints ensures efficiency and scalability. For advanced use cases, explore the full RPC method list.