Skip to main content

Connect over WebSocket

Overview

Ronin WebSockets allows you to receive real-time updates about events on Ronin by establishing a persistent connection over the WebSocket protocol. This can be useful for building interactive and responsive decentralized applications (dApps).

info

Until further notice, only Ethereum namespaces are allowed in the Ronin WebSockets connection.

Prerequisites

Subscribe and retrieve messages

The following code show how to connect to Ronin WebSockets using the ws library and your API key, and how to retrieve messages.

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

The following is a list of events for which you can listen when connected to Ronin through the WebSocket protocol.

newHeads

Emits a notification each time a new header is appended to the chain, including chain reorganizations.

Example message:

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

Response:

{
"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

Returns the hash for all transactions that are added to the pending state and are signed with a key that is available in the node.

Example message:

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

Response:

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

logs

Returns logs that are included in new imported blocks and match the given filter criteria.

Example messages:

  • Subscribe to Transfer() events of a WETH contract.
{  
"method": "eth_subscribe",
"params": [
"logs",
{
"address": "0xc99a6A985eD2Cac1ef41640596C5A5f9F4E19Ef5",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}
],
"id": 1,
"jsonrpc": "2.0"
}
  • Subscribe to logs from block 0 to block 100m.
{  
"method": "eth_subscribe",
"params": [
"logs",
{
"fromBlock": "0x0",
"toBlock": "0x5F5E100"
}
],
"id": 1,
"jsonrpc": "2.0"
}

Responses:

{
"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 helpful?