Skip to main content

Connect over WebSocket

Overview

Ronin WebSockets facilitate a persistent, real-time data stream directly from the blockchain, enabling your decentralized applications (dApps) to respond instantly to on-chain events. This is essential for dApps that thrive on immediacy and interactivity, such as gaming platforms or trading apps.

Note: Connections are limited to Ethereum namespaces within the Ronin WebSockets infrastructure.

Prerequisites

  • API key for authenticating your requests. Retrieve your API key from the Developer Console > your app > Information > KEY section.
  • WebSocket service access. Access must be granted to interact with the WebSocket service.

For more information, see Get started.

Subscribe and retrieve messages

To engage with Ronin WebSockets, use the following JavaScript example, which utilizes the ws library to establish a connection and handle incoming data.

const WebSocket = require('ws')
const options = {
headers: {
'X-API-KEY': "YOUR-API-KEY"
}
}
const ws = new WebSocket('wss://api-gateway.skymavis.com/rpc/socket', options)
const message = {
"method": "eth_subscribe",
"params": [
"newHeads"
],
"id": 1,
"jsonrpc": "2.0"
}

ws.on('open', function open() {
ws.send(JSON.stringify(message));
});

ws.on('message', function message(data) {
console.log('received: %s', data);
});

Subscription types

Explore the range of subscription events available on Ronin WebSockets, enabling diverse functionalities and real-time responses for your dApps.

newHeads

Receives notifications each time a new block header is added to the blockchain, crucial for applications that need to monitor block confirmations and reorganizations.

Example and response:

{  
"method": "eth_subscribe",
"params": [
"newHeads"
],
"id": 1,
"jsonrpc": "2.0"
}

{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0xeb7dcdfe2bfc5e0bbcf2b776ba681d3c",
"result": {
"parentHash": "0x961d7ae5602fdf5da4425844c25d56f3e230706b5435269af3e7c0c426e9c680",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0xae53daac1bf3c4633d4921b8c3f8d579e757f5bc",
"stateRoot": "0xa581fd1d5b0f99f37ee267c4c0946d9dd125398ccb5a71fbcb7a0203329aae02",
"transactionsRoot": "0xb0c66c47c42d8f641e2197a43194a227a3aa86df791974a7528a6589a5809f21",
"receiptsRoot": "0x9f7532c324f8ca7f0b1a0a3ebd7aee11a7646266e04da2a773a6fdbae8356335",
"logsBloom": "0x00000000080000000000000000000000100000080000004000000000000000000800800000002000002001000000800000404000000000000000000000080004000000000000000000000008000000000000600000800000000000000000000000000000000001000000000004000000000080000000000000000014000000200100000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000200000000000000000000822000000000000020000000000000000000000000010000000000a00000000200000000000000000000000000000000800000000000040000000000000",
"difficulty": "0x7",
"number": "0x177a012",
"gasLimit": "0x5f5e100",
"gasUsed": "0x2ac19",
"timestamp": "0x6479637c",
"extraData": "0xd683020502846765746886676f312e3137856c696e757800000000000000000073aa5aff3ad3a20354f3ec7d30a630d26cd027366a6eec125a4833f6649cbc8f09ce0bbad23ebb679e69e143666a17098b6bfd221c66e65368269f736a4df89a00",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"baseFeePerGas": null,
"hash": "0xbd13f924e30908b63e14c49890fc32efec1de3c3131e9e6af28b347a75f33ade"
}
}
}

newPendingTransactions

Tracks new transactions as soon as they are added to the pending pool, vital for apps that need to respond quickly to transaction submissions.

Example and response:

{  
"method": "eth_subscribe",
"params": [
"newPendingTransactions"
],
"id": 1,
"jsonrpc": "2.0"
}

{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0xec9cc07bfb60a076695ce1999dba59db",
"result": "0xf0887b1b728e5ecaba5678f8829eb80089f99c190fbf943f5fadb7108b9a0955"
}
}

logs

Monitor specific contract logs in real-time, essential for applications that need to track specific events such as transfers or updates within contracts.

Example and responses:

  • Subscribe to Transfer() events of a specific contract:
{  
"method": "eth_subscribe",
"params": [
"logs",
{
"address": "0xc99a6A985eD2Cac1ef41640596C5A5f9F4E19Ef5",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}
],
"id": 1,
"jsonrpc": "2.0"
}

Response:

{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0x98a829e969ef490f074bc445ba5363b7",
"result": {
"address": "0x814a9c959a3ef6ca44b5e2349e3bba9845393947",
"topics": [
"0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62",
"0x000000000000000000000000fff9ce5f71ca6178d3beecedb61e7eff1602950e",
"0x000000000000000000000000cfe5b7b3502145c62d41492c7f04c1a6dbe29076",
"0x00000000000000000000000099cf2aa4177984b1c437304ae963a88eeb2066d8"
],
"data": "0x00000000000000000000000000000000000000000000000000000000001e94500000000000000000000000000000000000000000000000000000000000000001",
"blockNumber": "0x177a268",
"transactionHash": "0x7be22488e41e395a81b717a4969b258b59f43d9caa6a853e293a7117cacbadf0",
"transactionIndex": "0x3",
"blockHash": "0xafca38bcea645165e6bceb8ea3c6eda6e37686bec7596e7fc797baf1a25611bd",
"logIndex": "0x8",
"removed": false
}
}
}

See also

Ronin JSON-RPC API reference

Was this page helpful?
Happy React is loading...