Connecting WebSocket
Public Node API provides features identical to KAS Node API without a separate authentication process. You can execute API features by connecting WebSocket.
Public Node API may provide unstable services depending on the traffic state. We recommend KAS Klaytn Node API for a stable environment using WebSocket.
This page describes the feature of WebSocket, which Public Node API supports. You can establish a two-way handshake between you and Endpoint Node. Unlike Klaytn Node API, Public Node API doesn't specify network by x-chain-id, and KAS Credentials are also not required. It configures its path according to the Klaytn network. Enter
cypress
for Klaytn mainnet and baobab
for Klaytn testnet. Please note that code snippets on this page connect to Baobab network.wscat --connect "wss://public-node-api.klaytnapi.com/v1/baobab/ws"
The ping deadline for WebSocket connection is limited to 60 seconds.
You need the latest block number on Klaytn to check the most recent data about your account, such as account balance and account key type. Connect to WebSocket and request the newest block number as follows.
// Request
wscat --connect "wss://public-node-api.klaytnapi.com/v1/baobab/ws"
> {"jsonrpc":"2.0", "id": 0.43, "method": "klay_blockNumber", "params": [ ] }
// Response
< {"jsonrpc":"2.0","id":0.43,"result":"0x4d418ff"}
Using
klay_subscribe
, you can create a new subscription to specific events. Every subscription returns an ID, and the notification contains all event data and subscription ID. If your notification parameter is "NewHeads"
, it notifies whenever a new block is added; another parameter "logs"
notifies log data on a new block. The subscription will be disconnected and removed when no ping or request is made within 60 seconds.The sample below shows the request and response with
"NewHeads"
parameter. To input "logs"
, refer to [klay_newFilter] (https://docs.klaytn.foundation/dapp/json-rpc/api-references/klay/filter#klay_newfilter) to check the required objects.// Request
wscat --connect "wss://public-node-api.klaytnapi.com/v1/baobab/ws"
> {"jsonrpc":"2.0", "id": 1, "method": "klay_subscribe", "params": ["newHeads"]}
// Response
< {"jsonrpc":"2.0","id":1,"result":"0x902e07c8168b5d06b29fa60f2eaa7630"}
< {"jsonrpc":"2.0","method":"klay_subscription","params":{"subscription":"0x902e07c8168b5d06b29fa60f2eaa7630","result":{"parentHash":"0xd9e0ac7ba8dc39562fa511bfd8b0972024105f1ccdcd24bf5cf7f961ac630b7b","reward":"0xa86fd667c6a340c53cc5d796ba84dbe1f29cb2f7","stateRoot":"0x20f866c3a42228d268a11abaf989ae8c95050cae25031dbe5a3833e3c7f672f3","transactionsRoot":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","receiptsRoot":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockScore":"0x1","number":"0x4d41913","gasUsed":"0x0","timestamp":"0x61e7befb","timestampFoS":"0x0","extraData":"0xd883010703846b6c617988676f312e31352e37856c696e757800000000000000f90164f85494571e53df607be97431a5bbefca1dffe5aef56f4d945cb1a7dccbd0dc446e3640898ede8820368554c89499fb17d324fa0e07f23b49d09028ac0919414db694b74ff9dea397fe9e231df545eb53fe2adf776cb2b84164cd25226ad8fb9d70f9ad29ca5e595d5ceaa2e6fa4e1cb58b082d541aa7cf8d587cca8dacd2809bf6bbc9a12ce1c759ffa3378ee06de594449bd921c9f52fad01f8c9b841284bcf0b9c8c315bb77071f0e275bf1491bf7d101bdd355dae35394e95346265171a15ce72417ecc6d13769101af7fe5637cedb149c19f46d13ca850d1dd415b01b841de55d3afe8c6be9a0583c1dc5b330f0992498b2d3103c935ee88e3e6f098526a3d17c8be0223fa2dc7f81f8dda71bdca248b3b9a40cdab708035265fe6ef3b7601b841144fc3a5d71601183cb8af4f173837af0fa75d47d4cba16ab0d2a7f3daa3234b7bdd52cc14e5a673e1aaf00a650d6c7359cf26697d0dfb911105be04e5cbe09600","governanceData":"0x","hash":"0xda0c5d55d4ce2902e3967e33a5b12e2e33e2b3e8e4a8e8afe95abfdcd0c12468"}}}
< {"jsonrpc":"2.0","method":"klay_subscription","params":{"subscription":"0x902e07c8168b5d06b29fa60f2eaa7630","result":{"parentHash":"0xda0c5d55d4ce2902e3967e33a5b12e2e33e2b3e8e4a8e8afe95abfdcd0c12468","reward":"0xb2bd3178affccd9f9f5189457f1cad7d17a01c9d","stateRoot":"0x97614a5c4ced0651ad6aad95e902a9e339e5f0c9432d160a041ba50a27cea50d","transactionsRoot":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","receiptsRoot":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockScore":"0x1","number":"0x4d41914","gasUsed":"0x0","timestamp":"0x61e7befc","timestampFoS":"0x0","extraData":"0xd883010703846b6c617988676f312e31352e37856c696e757800000000000000f90164f85494571e53df607be97431a5bbefca1dffe5aef56f4d945cb1a7dccbd0dc446e3640898ede8820368554c89499fb17d324fa0e07f23b49d09028ac0919414db694b74ff9dea397fe9e231df545eb53fe2adf776cb2b841248b924354b2dcfc7bdbe6d68e7179cf9fcdda9674cc88d803fc2feb2e0cc76d4c8d8c8aaba2eb6264ea7b4a886ffdbec7b26a6050409c54ff1e079df2cce75f00f8c9b841693c7a16935a600c84561002dbca03da61f1671ea5823c671162baa95acfea143c942b0602e43629d27bffee7d72f53c071a00387df1f9553f4f1dc265f23c4500b841b5ce1989697c63e294ddac38746c84f2ba135e95fa30a874a306b4d23c9304db1225011ff3c3b738cf0f5477526a4dc3729f5dc600e1848f9e39e66be2099bc401b841eea1fcf44edbe41a0c97b352bff4cb5169d1018bec32f8f546304c5efa20eae2188b8160daed4c54f741fd5925a1a3861da3dfd8bcebf764e71b4b343bb6ff1701","governanceData":"0x","hash":"0xd7063d0f9c5d11e37fd991bcd60b49ce9f190824f54110bf526ad27415b09fa6"}}}
klay_unsubscribe
makes you stop observing the specific event notifications by calling klay_subscribe
. Input an ID as a parameter to specify the event you want to unsubscribe. When the subscription is completely canceled, it returns true as a result.// Request
wscat --connect "wss://public-node-api.klaytnapi.com/v1/baobab/ws"
> {"jsonrpc":"2.0", "id": 1, "method": "klay_unsubscribe", "params": ["0x902e07c8168b5d06b29fa60f2eaa7630"]}
// Response
< {"jsonrpc":"2.0","id":1,"result":true}
Last modified 5mo ago