Websocket
Creating a subscription
Streams are available with Websocket via both Gateway and Cloud-API. Subscriptions are created using the RPC call subscribe
with the stream (feed) name and subscription options set as parameters. Each stream has a different subscription name and can accept different subscription options. Please refer to the specific stream page for info.
subscribe
returns a subscription ID that will be paired with all related notifications.
WebSocket connection
Before using the subscribe call, you need to open a Websocket connection to the Gateway or the Cloud-API.
Notes:
We assume that the Gateway IP is 127.0.0.1 with default ws port 28333 in the example below.
ws://127.0.0.1:28333/ws
Examples
Subscribing to Gateway Stream in Python (version 3.7 or higher):
import asyncio, websockets, json
async def main():
try:
auth_key = "YOUR_AUTHORIZATION_HEADER"
async with websockets.connect(
'ws://127.0.0.1:28333/ws',
additional_headers={"Authorization" : auth_key},
) as websocket:
request = json.dumps({"id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]})
websocket.send(request)
while True:
response = json.loads(websocket.recv())
print(response) # or process it generally
except Exception as e:
print(f'Connection failed, Reason: {e}')
if __name__ == '__main__':
asyncio.run(main())
For more help subscribing to a Websocket stream, follow our step-by-step video walkthrough.
Last updated