WebSockets
WebSockets
What are WebSockets?
The WebSockets (WSS) communication protocol enables two-way communication between a client and a server over a single TCP connection. The communication protocol maintains a network connection between the two parties, allowing for real-time, low-latency communication. WebSockets allow for ongoing, bidirectional communication unlike HTTP, which is a request-response protocol.
Subscriptions over WebSockets
WebSockets allow you to create stateful subscriptions, which is a type of subscription where the server maintains a record of the client's subscription state. This means that the server remembers what data the client has requested and sent previously, and only sends new data that has changed or meets the client's subscription criteria.
You can create stateful subscriptions over WebSockets to subscribe to specific events on the blockchain. The following subscription methods are available:
eth_subscribe
- Create a subscription to a particular eventeth_unsubscribe
- Cancel an active subscription
Supported networks
Supported chains: ETH, ETC, BSC, KCC.
eth_subscribe
Creates a new subscription for particular events. The node returns a subscription ID. For each event that matches the subscription, a notification with relevant data is sent together with the subscription ID.
Parameters
Specify the subscription event
parameters, including the following:
newHeads
: Subscribing to this returns a notification each time a new header is appended to the chain, including chain reorganizations. In a chain reorganization, the subscription emits all new headers for the new chain. Therefore the subscription can emit multiple headers at the same height.logs
: Returns logs that are included in new imported blocks and match the given filter criteria. In case of a chain reorganization, previously sent logs that are on the old chain are resent with the removed property set totrue
. Logs from transactions that ended up in the new chain are emitted. Therefore a subscription can emit logs for the same transaction multiple times. This parameter has the following fields:address
: (optional) Either an address or an array of addresses. Only logs that are created from these addresses are returned.topics
: (optional) Only logs that match these specified topics are returned.
❗️ We strongly recommend specifying a filter (address
or topics
or both) when subscribing to the logs
event.
newPendingTransactions
: Returns the hash for all transactions that are added to the pending state and are signed with a key that's available in the node. When a transaction that was previously part of the canonical chain isn't part of the new canonical chain after a reorganization, it's emitted again.
Returns
- subscription ID: The ID of the newly created subscription on the node.
Considerations
- Notifications are sent for current events and not for past events. For use cases that cannot afford to miss any notifications, subscriptions are probably not the best option.
- Subscriptions are coupled to a connection. If the connection is closed all subscriptions that are created over this connection are removed.
- Notifications are stored in an internal buffer and sent from this buffer to the client. If the client is unable to keep up and the number of buffered notifications reaches a limit (currently 10k) the connection is closed. Keep in mind that subscribing to some events can cause a flood of notifications, e.g. listening for all logs/blocks when the node starts to synchronize.
Example
Replace YOUR-APIKEY
with the APIkey from your Tokenview Developer Dashboard.
❗️ WEBSOCKET CONNECTIONS ONLY
Subscription methods are available for WebSocket connections only.
🚀 Create subscription(newHeads):
wscat -c wss://services.tokenview.io/vipapi/websocket/node/{network}/{apikey} -x '{"jsonrpc":"2.0", "id": 1, "method": "eth_subscribe", "params": ["newHeads"]}'
Response
Return a subscription id:
{ "id": 1, "jsonrpc": "2.0", "result": "0xe67a687023bdc1e8c6cdb07d8826954d" }
After the creation of the subscrition, we can receive incoming notifications related to this subscription:
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params":
{
"subscription": "0xe67a687023bdc1e8c6cdb07d8826954d",
"result":
{
"parentHash": "0xa589086e591184920f584eb640229765efe36c291c8020e9a34b05ec901d4294",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0xbaf6dc2e647aeb6f510f9e318856a1bcd66c5e19",
"stateRoot": "0x6e5de1f9bd9bf5ddb6d744910e8b12c4653fcfd15107854efe175bad15c8a08d",
"transactionsRoot": "0x2b42d54110acafed7634066dbd12aa625ac76faa721445cc70d9a03fae1ae70c",
"receiptsRoot": "0x26e2b43c0c1625380667966feb19b6beed2bc0cd9e56355b200629db47a107ea",
"logsBloom": "0x12b13006554af51912056121a2501001b0fc0581a214261b0c03f8625684036084048bd9ec30927567109b6c64ba51620325c159baab38b953356b21e2790118e630ce48cf6abd284a2ecabc991340b49692402940f4d8f46bbaceecf9649abcbb7d23a0b34b0e160337b2b64c49da71caea61328c2c6c3a8241311843ec911fb823a2d413044249685a35dd091f91568c1ad261a989063d4c3129e6915812113b3900708a2afa202aa248e6dc98c2862452a0a3e42c46a914222178015a26b0d4d58412208404c7c484842874864a1c65f761027061313c12952e025173bcc00871a80a1d06449444c916837180510875a11759d33804de0d32382934243e1f",
"difficulty": "0x0",
"number": "0x1119dbd",
"gasLimit": "0x1c9c380",
"gasUsed": "0xc73b69",
"timestamp": "0x64dd8cc3",
"extraData": "0x4d616465206f6e20746865206d6f6f6e20627920426c6f636b6e6174697665",
"mixHash": "0x8a184cd85de29a989f391298fca3ba31f3dac6ac2c3b0dee6fab66d38d15d31d",
"nonce": "0x0000000000000000",
"baseFeePerGas": "0x51b4f5254",
"withdrawalsRoot": "0xc1a20e41d404dd67443f2e176b4127b55bb5f0baa0597875b62c96b4c6b4b29a",
"hash": "0x1eb86bd79d4b35e9087c09e909a9612972bce66ae09b1eba7cd80385ee137257"
}
}
}
🚀 create subscription (logs):
wscat -c wss://services.tokenview.io/vipapi/websocket/node/{network}/{apikey} -x '{"jsonrpc":"2.0", "id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd", "topics":["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"]}]}'
Response
{ "id": 2, "jsonrpc": "2.0", "result": "0x4a8a4c0517381924f9838102c5a4dcb7" }
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0x4a8a4c0517381924f9838102c5a4dcb7",
"result": {
"address": "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd",
"blockHash": "0x61cdb2a09ab99abf791d474f20c2ea89bf8de2923a2d42bb49944c8c993cbf04",
"blockNumber": "0x29e87",
"data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003",
"logIndex": "0x0",
"topics": ["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"],
"transactionHash": "0xe044554a0a55067caafd07f8020ab9f2af60bdfe337e395ecd84b4877a3d1ab4",
"transactionIndex": "0x0"
}
}
}
🚀 create subscription (newPendingTransactions):
wscat -c wss://services.tokenview.io/vipapi/websocket/node/{network}/{apikey} -x '{ "id": 1, "jsonrpc": "2.0", "method": "eth_subscribe", "params": ["newPendingTransactions"] }'
Response
{ "id": 1, "jsonrpc": "2.0", "result": "0xc3b33aa549fb9a60e95d21862596617c" }
{
"jsonrpc":"2.0",
"method":"eth_subscription",
"params":{
"subscription":"0xc3b33aa549fb9a60e95d21862596617c",
"result":"0xd6fdc5cc41a9959e922f30cb772a9aef46f4daea279307bc5f7024edc4ccd7fa"
}
}
eth_unsubscribe
Cancel subscriptions by calling this method with the subscription ID. It returns a boolean indicating that the subscription was canceled successfully.
Parameters
subscription ID
: The ID of the subscription you want to unsubscribe.
Returns
unsubscribed flag
: (boolean)True
if the subscription is canceled successfully.
Example
Request
wscat -c wss://services.tokenview.io/vipapi/websocket/node/{network}/{apikey} -x '{"jsonrpc":"2.0", "id": 1, "method": "eth_unsubscribe", "params": ["0x9cef478923ff08bf67fde6c64013158d"]}'
Response
{
"id": 1,
"jsonrpc": "2.0",
"result": true
}