Use WebSockets
Most of the methods exposed by the Blast API endpoints can be queried with simple HTTP requests that follow the protocol of the network. However, one thing to note is that each HTTP request sent to our endpoints goes through a load-balancer and, as such, two consecutive requests might not hit the same node. This might result in surprising results such as asking what the head block is then trying to fetch information about it might fail since the first request might reach one node that is synced ahead of the others while the second request might reach other nodes that have not synced that block yet.
One solution to this problem is to use a persistent connection to the node and you can do that using WebSockets.
In order to create a WebSocket connection to an endpoint, you need to use the endpoint URL starting with
wss://
. The WebSocket client will send an HTTP request with an Upgrade header that basically tells the server that the client wants to upgrade this HTTP connection (which is stateless by default and closed once the server answers to the request) to a WebSocket connection. The server will then reply with status 101 Switching Protocols
and then both the client and the server will have switched to the WebSocket protocol.If you want to quickly test some commands, wscat is one of the most popular command-line client.
$ wscat -c wss://eth-mainnet.blastapi.io/${projectId}
Connected (press CTRL+C to quit)
> {"jsonrpc":"2.0","id":0,"method":"eth_blockNumber","params":[]}
< {"jsonrpc":"2.0","id":0,"result":"0xdc02b0"}
$ wscat -c wss://eth-mainnet.blastapi.io/${projectId}
Connected (press CTRL+C to quit)
> {"jsonrpc":"2.0","id":0,"method":"eth_subscribe","params":["newHeads"]}
< {"jsonrpc":"2.0","id":0,"result":"0x720d759d25a2d00b91158655d4cd0e2c"}
< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x720d759d25a2d00b91158655d4cd0e2c","result":{"parentHash":"0x02fc98fa360d6bc7c86f915e89c896da4f98bcd46eddd3273ae1e10a557d96eb","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x7f101fe45e6649a6fb8f3f8b43ed03d353f2b90c","stateRoot":"0x7bc4a38040636b7cb2f7a244470f97d306c862e391f9bf47096bacca149c3a41","transactionsRoot":"0x1c7e9dce8d92b4c3e95ae47b3941a48a5ac71b5fb592b0a062a1c71d2f9a674b","receiptsRoot":"0x76b91817dd9e69d94feabb1ef251bbcbc7dbeaf38ac0a5a97fc500b16a27d4b5","logsBloom":"0x64e531e3a950d02571aaa11cb19dd66d0b2a932a9dd1c5d79195406fbec3096f01ad78c38c8cecf16511eef723dba772fb600e011851741d23d00f6a932b72c876aef49854610e3f6c46618b76c077a433d34d109a40540bde61e25392c4de210a418a60a229b0430ef3b560d0a02fdc608bddd6958cbf6169b831394c4b325c2831e171a343f54d5578862f1d40aa776993b9071737a27cd3e60d52aedbbcf5370e1d4f69807a97e576c9d1c78ad575af77f1c740c601eb97f5b3dbdc6c589931eb665a4549cc4d254450cea1e7d0c4b5ae930a1f8fe61015c30146c3233127ab1b6569df6a5902a1e794001761eef3d37589497b6701411a110b634007470b","difficulty":"0x2fc2dafe16c123","number":"0xdc02b8","gasLimit":"0x1cc1387","gasUsed":"0x14f4df2","timestamp":"0x62362698","extraData":"0x466c6578706f6f6c2f53612f4445202d204361706520546f776e","mixHash":"0xf22c51f1fc19a742473818d208e8563a10fa8cc668dc19d82dfff91693c06b95","nonce":"0xdcd0d592fda8c8b1","baseFeePerGas":"0x52959fe79","hash":"0xd7757a2d7d6f76a2f378744180e31ebd3547c16c0a000c2797cfd44f75b60875"}}}
> {"jsonrpc":"2.0","id":0,"method":"eth_unsubscribe", "params": ["0x720d759d25a2d00b91158655d4cd0e2c"]}}
< {"jsonrpc":"2.0","id":0,"result":true}
Last modified 5d ago