Websocket
Cancelling a subscription
If you need to cancel a subscription, pass the subscription ID to the unsubscribe
call.
Example
Unsubscribe call example for the Gateway in Python (version 3.7 or higher):
import asyncio, ssl, json, websockets
async def main():
auth_key = "YOUR_AUTHORIZATION_HEADER"
async with websockets.connect(
'ws://127.0.0.1:28333/ws',
header=["Authorization:{}".format(auth_key)],
sslopt={"cert_reqs": ssl.CERT_NONE},
) as websocket:
subscribe_request = {
"jsonrpc": "2.0",
"id": 1,
"method": "subscribe",
"params": ["newTxs", {"include": ["tx_hash"]}]
}
await websocket.send(json.dumps(subscribe_request))
response = await websocket.recv()
subscription_id = json.loads(response)["result"]
unsubscribe_request = {
"jsonrpc": "2.0",
"id": 2,
"method": "unsubscribe",
"params": [subscription_id]
}
await websocket.send(json.dumps(unsubscribe_request))
response = await websocket.recv()
print("Unsubscribed successfully.")
if __name__ == '__main__':
asyncio.run(main())
Unsubscribe call example for the Cloud-API in Python (version 3.7 or higher):
import asyncio, ssl, json, websockets
async def main():
auth_key = "YOUR_AUTHORIZATION_HEADER"
async with websockets.connect(
'wss://virginia.eth.blxrbdn.com/ws',
header=["Authorization:{}".format(auth_key)],
sslopt={"cert_reqs": ssl.CERT_NONE},
) as websocket:
subscribe_request = {
"jsonrpc": "2.0",
"id": 1,
"method": "subscribe",
"params": ["newTxs", {"include": ["tx_hash"]}]
}
await websocket.send(json.dumps(subscribe_request))
response = await websocket.recv()
subscription_id = json.loads(response)["result"]
unsubscribe_request = {
"jsonrpc": "2.0",
"id": 2,
"method": "unsubscribe",
"params": [subscription_id]
}
await websocket.send(json.dumps(unsubscribe_request))
response = await websocket.recv()
print("Unsubscribed successfully.")
if __name__ == '__main__':
asyncio.run(main())
Unsubscribe call example for the Cloud-API in Python (version 3.7 or higher):
import asyncio, ssl, json, websockets
async def main():
auth_key = "YOUR_AUTHORIZATION_HEADER"
async with websockets.connect(
'wss://api.blxrbdn.com/ws',
header=["Authorization:{}".format(auth_key)],
sslopt={"cert_reqs": ssl.CERT_NONE},
) as websocket:
subscribe_request = {
"jsonrpc": "2.0",
"id": 1,
"method": "subscribe",
"params": ["newTxs", {"include": ["tx_hash"]}]
}
await websocket.send(json.dumps(subscribe_request))
response = await websocket.recv()
subscription_id = json.loads(response)["result"]
unsubscribe_request = {
"jsonrpc": "2.0",
"id": 2,
"method": "unsubscribe",
"params": [subscription_id]
}
await websocket.send(json.dumps(unsubscribe_request))
response = await websocket.recv()
print("Unsubscribed successfully.")
if __name__ == '__main__':
asyncio.run(main())
Last updated